pandas使用跨多个列的字典值进行乘法运算

bla*_*ite 10 python numpy pandas

给出以下数据帧:

import pandas as pd
df = pd.DataFrame({
    'a': [1,2,3,4,5],
    'b': [5,4,3,3,4],
    'c': [3,2,4,3,10],
    'd': [3, 2, 1, 1, 1]
})
Run Code Online (Sandbox Code Playgroud)

以及以下参数列表:

params = {'a': 2.5, 'b': 3.0, 'c': 1.3, 'd': 0.9}
Run Code Online (Sandbox Code Playgroud)

产生以下所需的输出:

   a  b   c  d  output
0  1  5   3  3    24.1
1  2  4   2  2    21.4
2  3  3   4  1    22.6
3  4  3   3  1    23.8
4  5  4  10  1    38.4
Run Code Online (Sandbox Code Playgroud)

我一直用它来产生结果:

df['output'] = [np.sum(params[col] * df.loc[idx, col] for col in df)
                 for idx in df.index]
Run Code Online (Sandbox Code Playgroud)

但是,这是一种非常慢的方法,我认为必须有一种更好的方法来使用内置的pandas功能.

我也想到了这个:

# Line up the parameters
col_sort_key = list(df)
params_sorted = sorted(params.items(), key=lambda k: col_sort_key.index(k[0]))

# Repeat the parameters *n* number of times
values = [v for k, v in params_sorted]
values = np.array([values] * df.shape[0])

values
array([[ 2.5,  3. ,  1.3,  0.9],
       [ 2.5,  3. ,  1.3,  0.9],
       [ 2.5,  3. ,  1.3,  0.9],
       [ 2.5,  3. ,  1.3,  0.9],
       [ 2.5,  3. ,  1.3,  0.9]])

# Multiply and add
product = df[col_sort_key].values * values
product
array([[  2.5,  15. ,   3.9,   2.7],
       [  5. ,  12. ,   2.6,   1.8],
       [  7.5,   9. ,   5.2,   0.9],
       [ 10. ,   9. ,   3.9,   0.9],
       [ 12.5,  12. ,  13. ,   0.9]])

np.sum(product, axis=1)
array([ 24.1,  21.4,  22.6,  23.8,  38.4])
Run Code Online (Sandbox Code Playgroud)

但这似乎有点令人费解!对本地熊猫的任何想法都试试?

jez*_*ael 6

你可以使用assign+ mul+ sum:

df1 = df.assign(**params).mul(df).sum(1)
print (df1)
0    24.1
1    21.4
2    22.6
3    23.8
4    38.4
dtype: float64
Run Code Online (Sandbox Code Playgroud)

dot+ Series构造函数:

df1 = df.dot(pd.Series(params))
print (df1)
0    24.1
1    21.4
2    22.6
3    23.8
4    38.4
dtype: float64
Run Code Online (Sandbox Code Playgroud)

  • 请注意,对于“df1 = df.dot(pd.Series(params))”方法**参数字典的顺序很重要**。点积不考虑列名称,只考虑列顺序。 (2认同)

WeN*_*Ben 6

(pd.Series(params)*df).sum(1)
Out[816]: 
0    24.1
1    21.4
2    22.6
3    23.8
4    38.4
dtype: float64
Run Code Online (Sandbox Code Playgroud)

更多信息:

pd.Series(params)
Out[817]: 
a    2.5
b    3.0
c    1.3
d    0.9
dtype: float64

(pd.Series(params)*df)
Out[818]: 
      a     b     c    d
0   2.5  15.0   3.9  2.7
1   5.0  12.0   2.6  1.8
2   7.5   9.0   5.2  0.9
3  10.0   9.0   3.9  0.9
4  12.5  12.0  13.0  0.9
Run Code Online (Sandbox Code Playgroud)

对于你的榜样,您可以使用dot,以及

df.values.dot(np.array(list(params.values())))
Out[827]: array([ 24.1,  21.4,  22.6,  23.8,  38.4])
Run Code Online (Sandbox Code Playgroud)