【Pandas】根据某列分组求和

源数据:

data = [['API Management',10,"Apr-21"],['App Configuration',12,"Aug-21"],['Application Gateway',13,"Feb-21"],['Automation',13,"Apr-21"],['Azure Analysis Services',1,"Apr-21"]]
df = pd.DataFrame(data,columns=["serviceType", "cost", "date"],dtype=float)
print(df)

>>

               serviceType  cost    date
0           API Management  10.0  Apr-21
1        App Configuration  12.0  Aug-21
2      Application Gateway  13.0  Feb-21
3               Automation  13.0  Apr-21
4  Azure Analysis Services   1.0  Apr-21

根据date分组求和cost:

df=df.groupby(by=['date'])['cost'].sum().reset_index()
print(df)
>>
     date  cost
0  Apr-21  24.0
1  Aug-21  12.0
2  Feb-21  13.0

注:sum()后生成的数据类型是Series,如果进一步需要将其转换为dataframe,可以调用Series中的to_frame()方法或者reset_index().

上一篇:BZOJ 1592. Making the Grade(思维,数据结构优化DP,以及三个拓展问题)[Usaco2008 Feb]【BZOJ计划】


下一篇:python中列表(list)和元组(tuple)该如何抉择?有什么区别?