python - 在 Pandas 中加快处理速度
问题描述
我正在dataframe
垂直处理的一列上比较一个和三个其他的,我想知道这个过程是否可以使用更多的核心/让它更快?我试过concurrent.futures.ProcessPoolExecutor()
了,但实际上慢了 1 秒……这是我的代码
# df_out is main DataFrame, hikari_data_df, kokyaku_data_df, hikanshou_data_df are DF to compare
m1 = df_out[self.col_name_].isin(hikari_data_df['phone_num1'])
m2 = df_out[self.col_name_].isin(hikari_data_df['phone_num2'])
# Add new column to df_out on place of matching m1 with df_out col
df_out['new1'] = df_out[self.col_name_].where(m1)
df_out['new2'] = df_out[self.col_name_].where(m2)
m1 = df_out[self.col_name_].isin(kokyaku_data_df['phone_number1'])
m2 = df_out[self.col_name_].isin(kokyaku_data_df['phone_number2'])
df_out['new3'] = df_out[self.col_name_].where(m1)
df_out['new4'] = df_out[self.col_name_].where(m2)
m1 = df_out[self.col_name_].isin(hikanshou_data_df['phone_number'])
df_out['new5'] = df_out[self.col_name_].where(m1)
df_out.to_csv(sys.argv[1], index=False)
我希望这个过程更快!
解决方案
首先,如果您的数据不大。尝试将您的“isin”/“where”函数转换为“join/merge”等向量操作。这将花费更多内存,但速度更快。
其次,使用 dask。但是,要小心。如果你的数据不够大。Dask会变慢。