我有一个巨大的数据帧,我想merge only two rows in it based on if condition
。下面是一个示例数据帧,当我尝试执行groupby sum
时,其他行也会受到影响
我只希望带有generic
的列jb_name
得到merged and sum
jb_name jb_count
0 generic 10
1 generic1 2
2 generic 15
3 other 14
尝试了以下内容,但正如我所说,它也会影响其他行
df = df.groupby(['jb_name'])['jb_count'].sum().reset_index()
我想要最后的df如下
jb_name jb_count
0 generic 25
1 generic1 2
3 other 14
使用:
或者,我们可以将索引设置为
jb_name
,并在level 0
上使用sum
,其中索引为generic
:结果:
相关问题 更多 >
编程相关推荐