【发布时间】:2016-03-29 15:13:29
【问题描述】:
我有一个非常大的数据框
in>> all_data.shape
out>> (228714, 436)
我想要有效地做的是将许多列相乘。我从 for 循环和列列表开始——我发现最有效的方法是
from itertools import combinations
newcolnames=list(all_data.columns.values)
newcolnames=newcolnames[0:87]
#make cross products (the columns I want to operate on are the first 87)
for c1, c2 in combinations(newcolnames, 2):
all_data['{0}*{1}'.format(c1,c2)] = all_data[c1] * all_data[c2]
可能有人猜到的问题是我有 87 列,这将提供大约 3800 个新列(是的,这就是我的意图)。我的 jupyter notebook 和 ipython shell 都在这个计算中窒息。我需要想出一个更好的方法来进行这种乘法。
是否有更有效的矢量化和/或处理方式?也许使用 numpy 数组(我的数据框已经过处理,现在只包含数字和 NAN,它从分类变量开始)。
【问题讨论】:
标签: python pandas memory vectorization