庞达斯数据框架中的特定列
从下面提到的数据框架中,我试图根据S1,S2和S3来计算列V1,V2和V3列的Excel类型Sumproduct。
df = pd.DataFrame({'Name': ['A', 'B', 'C'],
'Qty': [100, 150, 200],
'Remarks': ['Bad', 'Avg', 'Good'],
'V1': [0,1,1],
'V2': [1,1,0],
'V3': [0,0,1],
'S1': [1,0,1],
'S2': [0,1,0],
'S3': [1,0,1]
})
我正在寻求一种方法来做到这一点,而不必使用每一列的名称,例如:
df['SP'] = df[['V1', 'S1']].prod(axis=1) + df[['V2', 'S2']].prod(axis=1) + df[['V3', 'S3']].prod(axis=1)
在我的真实数据框架中,我在“ V”和“ S”类别中都有50列以上,因此不可能使用上述方法。
有什么建议吗?
谢谢!
From the below mentioned data frame, I am trying to calculate excel type SUMPRODUCT of columns V1, V2 and V3 against columns S1, S2 and S3.
df = pd.DataFrame({'Name': ['A', 'B', 'C'],
'Qty': [100, 150, 200],
'Remarks': ['Bad', 'Avg', 'Good'],
'V1': [0,1,1],
'V2': [1,1,0],
'V3': [0,0,1],
'S1': [1,0,1],
'S2': [0,1,0],
'S3': [1,0,1]
})
I am looking a way to do this without having to use each column's name like:
df['SP'] = df[['V1', 'S1']].prod(axis=1) + df[['V2', 'S2']].prod(axis=1) + df[['V3', 'S3']].prod(axis=1)
In my real data frame, I have more than 50 columns in both 'V' and 'S' categories so the above approach is not possible.
Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然后过滤S和V类列,然后将S列与相应的V列相乘,并沿列轴PS总和结果
:该解决方案假设原始数据帧匹配中S和V列的外观顺序。
Filter the S and V like columns then multiply the S columns with the corresponding V columns and sum the result along columns axis
PS: This solution assumes that the order of appearance of S and V columns in the original dataframe matches.
您可以尝试这样的事情:
在看到@alollz的评论有关MultiIndex的评论后,用替代性编辑更简单:
然后,如果您愿意,则可以重置索引:
结果:结果:
You could try something like this:
Edited with an alternative after seeing comment from @ALollz about MultiIndex making alignment simpler:
You can then reset index if you prefer:
Results:
如果您的
vn
和列中的sn
If your
Vn
andSn
in columns are in order