[英]How to apply a function to a set amount of rows in a Dataframe?
I have the following code that uses nlp() on every column to determine the type.我有以下代码,它在每一列上使用 nlp() 来确定类型。 However, it could take a long time depending on the size of my data.
但是,这可能需要很长时间,具体取决于我的数据大小。 I was wondering how could I apply the function on selected amounts of rows?
我想知道如何在选定数量的行上应用 function? For example if I only wanted to apply it to the first 100 rows of every column instead?
例如,如果我只想将其应用于每列的前 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
Use the applymap
method to apply the function to all columns with a selected index range.使用
applymap
方法将 function 应用于具有选定索引范围的所有列。
For the first 100 rows:对于前 100 行:
df.iloc[:100] = df.iloc[:100].applymap(lambda x: [[w.label_] for w in list(nlp(str(x)).ents)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.