源数据:
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().