我有一个modin数据帧,它有120 K行。我想把它的几个栏目合并起来。Modin迭代花费了大量时间,所以我尝试使用numpy.where。Numpy.where是在相当于熊猫df在5-10分钟内完成,但同样的事情在摩丁df需要30分钟。对于modin dataframe来说,有其他方法来加速这个任务吗?
cols_to_be_coalesced ->此列表包含要合并的列列表。它包含10-15列.
代码:
for COL in [cols_to_be_coalesced]:
df['COL'] = np.where(df['COL']!='', df['COL'], df['COL_X'])
如果df是熊猫的dataframe,它在10分钟内执行,但如果是modin dataframe,则需要30分钟。那么,对于是否有任何等效的numpy.where代码来加速此操作?
发布于 2021-12-09 01:54:44
我认为您的np.where
速度很慢,因为np.where
将Modin转换为numpy数组,并且将Modin转换为numpy是慢的。这个版本使用pandas.Series.where
(不是Modin where
实现,因为还没有添加)对您来说更快吗?
for COL in [cols_to_be_coalesced]:
df['COL'] = df['COL'].where(df['COL'] != '', df['COL_X'])
在本例中,我发现该方法需要1.58秒,而原始方法则为70秒:
import modin.pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0, 100, size=(2**20, 2**8))).add_prefix("col")
# setting column with np.where takes 70 seconds
df['col1'] = np.where(df['col1'] % 2 == 0, df['col1'], df['col2'])
# setting column with pandas.Series.where takes 1.58 seconds
df['col1'] = df['col1'].where(df['col1'] % 2 == 0, df['col2'])
https://stackoverflow.com/questions/67224560
复制相似问题