繁体   English   中英

找到pandas中某些列的总和

[英]Find the sum of certain columns in pandas

我正在尝试使用pandas来对某些列进行求和,同时保留其他列。 例如:

member_no, data_1, data_2, data_3, dat_1, dat_2, other_1, other_2

1, 1, 3, 0, 0, 1, 1, 0

1, 1, 3, 0, 0, 1, 0, 1

2, 0, 1, 5, 1 ,0, 1, 0

2, 0, 1, 5, 1 ,0, 0, 1

我想要结果

member_no, data_1, data_2, data_3, dat_1, dat_2, other_1, other_2  

1, 1, 3, 0, 0, 1, 1, 1

2, 0, 1, 5, 1, 0, 1, 1

对于给定的成员id,具有'data'和'dat'的所有列将具有相同的值,因此我只想保留它。 具有“其他”属性的列需要求和。

谢谢您的帮助。

你在member_no + max上寻找一个groupby

df = df.groupby('member_no', as_index=False).max()
print(df)
   member_no  data_1  data_2  data_3  dat_1  dat_2  other_1  other_2
0          1       1       3       0      0      1        1        1
1          2       0       1       5      1      0        1        1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM