>>> df
A B C D E
0 one A foo 2.284039 0.802802
1 one B foo -1.463983 0.710178
2 two C foo -0.109677 2.930710
3 three A bar -0.356390 -1.972306
4 one B bar 1.425968 -0.285079
5 one C bar -0.657890 -0.555669
6 two A foo -0.168804 -1.930447
7 three B foo 0.488953 -2.512408
8 one C foo 0.251062 -0.465522
9 one A bar 0.427243 -0.845034
10 two B bar 0.629268 -0.892264
11 three C bar 0.171773 0.457268
我想获取D列的总和,以及E列的总和,其中A列为“ 1”,C列为“ foo”。
我知道这可行:
>>> x = df[df["A"] == "one"]
>>> y = x[x["C"] == "foo"]
>>> sum(y["D"])
1.0711178939632426
>>> sum(y["E"])
1.0474592505139344
是否有更紧凑/优雅的解决方案?
答案 0 :(得分:0)
使用熊猫,您可以这样做:
df.groupby(['A','C']).sum()
希望它对您有帮助。