Skip to main content
 首页 » 编程设计

python之大型数据框列乘法

2025年04月02日14findumars

我有一个非常大的数据框

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,它从分类变量开始)。

请您参考如下方法:

正如您在问题中提到的 NumPy,这可能是一个可行的选择,特别是因为您可能希望在 NumPy 的二维空间中工作,而不是使用 pandas 进行一维柱状处理。首先,您可以通过调用 np.array 将数据帧转换为 NumPy 数组,就像这样 -

arr = np.array(df) # df is the input dataframe 

现在,您可以获得列 ID 的成对组合,然后对列进行索引并执行列乘法,所有这些都将以矢量化方式完成,就像这样 -

idx = np.array(list(combinations(newcolnames, 2))) 
out = arr[:,idx[:,0]]*arr[:,idx[:,1]] 

sample 运行-

In [117]: arr = np.random.randint(0,9,(4,8)) 
     ...: newcolnames = [1,4,5,7] 
     ...: for c1, c2 in combinations(newcolnames, 2): 
     ...:     print arr[:,c1] * arr[:,c2] 
     ...:      
[16  2  4 56] 
[64  2  6 16] 
[56  3  0 24] 
[16  4 24 14] 
[14  6  0 21] 
[56  6  0  6] 
 
In [118]: idx = np.array(list(combinations(newcolnames, 2))) 
     ...: out = arr[:,idx[:,0]]*arr[:,idx[:,1]] 
     ...:  
 
In [119]: out.T 
Out[119]:  
array([[16,  2,  4, 56], 
       [64,  2,  6, 16], 
       [56,  3,  0, 24], 
       [16,  4, 24, 14], 
       [14,  6,  0, 21], 
       [56,  6,  0,  6]]) 

最后,您可以创建带有属性列标题的输出数据框(如果需要),就像这样 -

>>> headers = ['{0}*{1}'.format(idx[i,0],idx[i,1]) for i in range(len(idx))] 
>>> out_df = pd.DataFrame(out,columns = headers) 
>>> df 
   0  1  2  3  4  5  6  7 
0  6  1  1  6  1  5  6  3 
1  6  1  2  6  4  3  8  8 
2  5  1  4  1  0  6  5  3 
3  7  2  0  3  7  0  5  7 
>>> out_df 
   1*4  1*5  1*7  4*5  4*7  5*7 
0    1    5    3    5    3   15 
1    4    3    8   12   32   24 
2    0    6    3    0    0   18 
3   14    0   14    0   49    0