【问题标题】:How to apply a function to a set amount of rows in a Dataframe?如何将函数应用于数据框中的一组行?
【发布时间】:2020-10-26 01:13:06
【问题描述】:

我有以下代码,它在每一列上使用 nlp() 来确定类型。但是,这可能需要很长时间,具体取决于我的数据大小。我想知道如何将函数应用于选定数量的行?例如,如果我只想将其应用于每列的前 100 行?

import spacy
import pandas as pd
import en_core_web_sm
import numpy
nlp = en_core_web_sm.load()

df = pd.read_csv('https://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID=27211&Year=2019&Month=5&Day=1&timeframe=2&submit=Download+Data')

df['Station Name'] = df['Station Name'].str.title()

col_list = df.columns 

for col in col_list:
    df[col] = df[col].apply(lambda x: [[w.label_] for w in list(nlp(str(x)).ents)])

df

【问题讨论】:

  • 创建数据框的子集并进行处理。
  • 我会使用字典或列表chunk您的数据框,并处理每个字典条目:

标签: python pandas nlp


【解决方案1】:

使用applymap 方法将该函数应用于具有选定索引范围的所有列。

对于前 100 行:

df.iloc[:100] = df.iloc[:100].applymap(lambda x: [[w.label_] for w in list(nlp(str(x)).ents)])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 2021-09-10
    相关资源
    最近更新 更多