英文:
pandas groupby one column except if one other column has different entry
问题
我想要根据给定的列对数据框进行分组,但仅当另一列也相同时,并对另一列进行求和。给定这个示例:
test=pd.DataFrame({'A':['0','0','0','1'],'B':['AAA','AAA','BBB','CCC'],'C':[0.5,0.2,0.3,0.1]})
我想要对A进行分组,但只有当B不同时才进行分组。我想要的结果如下:
A B C
0 0 AAA 0.7
1 0 BBB 0.3
2 1 CCC 0.1
到目前为止,我还没有找到任何方法来实现这个。
英文:
I have a dataframe that I would like to group by a given column, BUT only if one other column is also the same, while doing a sum on an other column. Given this example:
test=pd.DataFrame({'A':['0','0','0','1'],'B':['AAA','AAA','BBB','CCC'],'C':[0.5,0.2,0.3,0.1]})
A B C
0 0 AAA 0.5
1 0 AAA 0.2
2 0 BBB 0.3
3 1 CCC 0.1
I would like group for A only if B is different. I am targeting following dataframe:
A B C
0 0 AAA 0.7
1 0 BBB 0.3
2 1 CCC 0.1
So far I did not find any way to do it
答案1
得分: 1
test=pd.DataFrame({'A':['0','0','0','1'],'B':['AAA','AAA','BBB','CCC'],'C':[0.5,0.2,0.3,0.1]})
test.groupby(['A','B'])['C'].sum()
A B
0 AAA 0.7
BBB 0.3
1 CCC 0.1
Name: C, dtype: float64
test.groupby(['A','B'], as_index=False)['C'].sum()
A B C
0 0 AAA 0.7
1 0 BBB 0.3
2 1 CCC 0.1
英文:
test=pd.DataFrame({'A':['0','0','0','1'],'B':['AAA','AAA','BBB','CCC'],'C':[0.5,0.2,0.3,0.1]})
test.groupby(['A','B'])['C'].sum()
A B
0 AAA 0.7
BBB 0.3
1 CCC 0.1
Name: C, dtype: float64
test.groupby(['A','B'], as_index=False)['C'].sum()
A B C
0 0 AAA 0.7
1 0 BBB 0.3
2 1 CCC 0.1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论