pandas按一列分组,除非另一列有不同的条目。

huangapple go评论75阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月15日 04:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定