要对多个条件进行分组,请传递列或条件的列表:
df['birthdate'].groupby([df.birthdate.dt.year, df.birthdate.dt.month]).agg('count')
例:
In [165]:df = pd.Dataframe({'birthdate':pd.date_range(start=dt.datetime(2015,12,20),end=dt.datetime(2016,3,1))})df.groupby([df['birthdate'].dt.year, df['birthdate'].dt.month]).agg({'count'})Out[165]: birthdate countbirthdate birthdate 2015 12 122016 1 31 2 29 3 1
更新
由于
0.23.0多索引级别名称必须唯一的限制,上述代码从版本开始不再起作用,您现在需要
rename级别才能使其起作用:
In[107]:df.groupby([df['birthdate'].dt.year.rename('year'), df['birthdate'].dt.month.rename('month')]).agg({'count'})Out[107]: birthdate countyear month 2015 12122016 1 31 2 29 3 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)