多个 DF 列的 Sumproduct

Sumproduct over multiple DF columns

ID    height_1    area_1   in_1    height_2    area_2   in_2    height_3    area_3   in_3
1     4           20       1       7           87       1       2          21       1
2     10          83       1       12          32       1       9          41       0
3     16          78       1       12          17       0       np.nan     np.nan   np.nan

如何用通用方法为每一行计算总和,如下所示...?

sumproduct = height_1 * area_1 * in_1 + height_2 * area_2 * in_2 + height_3 * area_3 * in_3 + ...  ```

例如row1 = 4 * 20 * 1 + 7 * 87 * 1 + 2 * 21 * 1

我的 Dataframe 大约是 `height_, area_, in_ 的 20 倍

df['sum'] = 0
for i in range(1, 21):
   df['sum'] += df['height_{}'.format(i)] * df['area_{}'.format(i)] * df['in_{}'.format(i)]

使用wide_to_longproductsum

s = (pd.wide_to_long(df, i='ID', j='val', 
                     stubnames=['height','area','in'] ,sep='_', suffix='\d+')
       .product(1).sum(level=0))

Out[575]:
ID
1     731.0
2    1214.0
3    1248.0
dtype: float64

将其分配给df:

df['sumproduct'] = df.ID.map(s)

df['sumproduct'] = s.values

df['sumproduct'] = s.to_numpy()