您可以将
agg列名作为键,将想要的函数作为值传递给字典。
import pandas as pdimport numpy as np# Create some randomised dataN = 20date_range = pd.date_range('01/01/2015', periods=N, freq='W')df = pd.Dataframe({'ages':np.arange(N), 'payments':np.arange(N)*10}, index=date_range)print(df.head())# ages payments# 2015-01-04 0 0# 2015-01-11 1 10# 2015-01-18 2 20# 2015-01-25 3 30# 2015-02-01 4 40# Apply np.mean to the ages column and np.sum to the payments.agg_funcs = {'ages':np.mean, 'payments':np.sum}# Groupby each individual month and then apply the funcs in agg_funcsgrouped = df.groupby(df.index.to_period('M')).agg(agg_funcs)print(grouped)# ages payments# 2015-01 1.5 60# 2015-02 5.5 220# 2015-03 10.0 500# 2015-04 14.5 580# 2015-05 18.0 540


