pandas 熊猫总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48957745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas sumproduct
提问by TylerNG
I have a df:
我有一个 df:
Value1 Value2 1 2 3
1 A 0 1 2
2 B 3 4 5
3 C 2 2 2
I want to perform sumproduct between the rows and the columns 1 2 3:
我想在行和列 1 2 3 之间执行 sumproduct:
Value1 Value2 1 2 3 sumproduct
1 A 0 1 2 8 = 0*1 + 1*2 + 2*3
2 B 3 4 5 26 = 3*1 + 4*2 + 5*3
3 C 2 2 2 12 = 2*1 + 2*2 + 2*3
I've tried (df.values*df.columns.values).sum(1)
but then I need to drop Value1
and Value2
columns first. Is there a better approach?
我已经试过了,(df.values*df.columns.values).sum(1)
但后来我需要先删除Value1
和Value2
列。有没有更好的方法?
Many thanks!
非常感谢!
采纳答案by biniow
df = pd.DataFrame({'1': [0, 3, 2], '2': [1, 4, 2], '3': [2, 5, 2]})
df['sumproduct'] = df[1] * 1 + df[2] * 2 + df[3] * 3
UPDATE for generic case
更新通用案例
valid_columns = [col for col in df.columns if col.isdigit()]
df['sumproduct'] = (df[valid_columns] * [int(x) for x in valid_columns]).sum(axis=1)
回答by YOBEN_S
What I will do
我将要做的
df.iloc[:,2:].dot([1,2,3])
Out[239]:
0 8
1 26
2 12
dtype: int64
To make it automatic
让它自动
s=df.iloc[:,2:]
s.dot(s.columns.astype(int))
Out[242]:
0 8
1 26
2 12
dtype: int64
回答by ilia timofeev
(df[['1','2','3']]*[1,2,3]).sum(axis=1)
Output:
输出:
0 8
1 26
2 12
Update:Universal approach
更新:通用方法
col = pd.to_numeric(df.columns,errors='coer')
(df[df.columns[~pd.isnull(col)]]*col.dropna()).sum(axis=1)
回答by David Leon
arr = [0]*len(df)
for i, v in enumerate([c for c in df.columns if not isinstance(c,str)]):
arr = arr + df[v]*v
df['sumproduct'] = arr
or even:
甚至:
cols = [c for c in df.columns if not isinstance(c, str)]
# or as @ilia
cols = pd.to_numeric(df.columns,errors='coerce').dropna()
df[cols].apply(lambda x: x*x.name).sum(axis='columns')