[英]How do to speed up ordinary dataframe loop in python? vectorisation? multiprocess?
我有一段簡單的代碼。 本質上,我想加快使用數據框創建數據框的循環。 我還沒有找到一個例子,並會感謝任何人的幫助。
df_new = []
for df_i in df:
df_selected = df[df['good_value'] == df_i_list]
df_new = pd.concat([df_new,df_selected])
鑒於您的代碼不起作用,這是我能想到的最好的。
從數據框列表開始,然后將數據框中的行選擇到另一個列表,然后一步連接。
由於 concat 是繁重的操作,因此可以確保您只調用它一次,這就是它的用途。
import pandas as pd
dfs = [df1, df2, df3, df4, ...]
sel = [df[df['column_to_filter'] == 'good_value'] for df in dfs]
df_new = pd.concat(sel) # might be useful to add `ignore_index=True`
df_new = df[df['good_value'].isin(df_i_list)]
pd.concat 比 .isin() 慢 4 倍
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.