簡體   English   中英

如何將 function 應用於 Dataframe 中的一組行?

[英]How to apply a function to a set amount of rows in a Dataframe?

我有以下代碼,它在每一列上使用 nlp() 來確定類型。 但是,這可能需要很長時間,具體取決於我的數據大小。 我想知道如何在選定數量的行上應用 function? 例如,如果我只想將其應用於每列的前 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

使用applymap方法將 function 應用於具有選定索引范圍的所有列。

對於前 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM