如何在多个按键上使用pandas Grouper? [英] How to use pandas Grouper on multiple keys?

查看:251
本文介绍了如何在多个按键上使用pandas Grouper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过datetime列和另一个str(object)列对数据帧进行分组转换,以按组应用功能,并将结果分配给该组的每个行成员。我了解 groupby工作流程,但不能创建 pandas.Grouper 同时满足这两个条件。因此:

I need to groupby-transform a dataframe by a datetime column AND another str(object) column to apply a function by group and asign the result to each of the row members of the group. I understand the groupby workflow but cannot make a pandas.Grouper for both conditions at the same time. Thus:

如何在多个列上使用 pandas.Grouper

推荐答案

pandas.Grouper DataFrame.groupby $ c>作为 by 参数,例如:

Use the DataFrame.groupby with a list of pandas.Grouper as the by argument like this:

df['result'] = df.groupby([
                 pd.Grouper('dt', freq='D'),
                 pd.Grouper('other_column')
               ]).transform(foo)

这篇关于如何在多个按键上使用pandas Grouper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆