Pandas Dataframe - 按照Col A分组并对每个组进行求和[C]重复

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有以下数据框:

COLA    COLB    COLC
a       cfg     100
b       gdd     100     
c       ert     100
d       yrt     100
a       yui     100
d       ouo     100
a       ooo     100
b       qwe     100

我想组合COLA中的所有项目(groupby?),然后在COLC中对它们的值求和,最后得到

COLA    COLC
a       300
b       200    
c       100
d       200

我尝试着:

df.groupby('COLA').sum('COLC')
python pandas m
1个回答
2
投票

GroupBy对象以与DataFrame相同的方式支持列索引,并返回已修改的GroupBy对象

import pandas as pd
df=pd.read_csv(path)
df.groupby('COLA')['COLC'].sum()
© www.soinside.com 2019 - 2024. All rights reserved.