Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pandas Grouper on multiple keys?

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:

How to use pandas.Grouper on multiple columns?

like image 542
pablete Avatar asked Sep 05 '18 14:09

pablete


1 Answers

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)
like image 144
pablete Avatar answered Oct 12 '22 15:10

pablete