22

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?

2 Answers 2

32

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)
Sign up to request clarification or add additional context in comments.

Comments

4

If your second column is a non-datetime series, you can group it with a date-time column like this:

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

Note that in this case you don't have to use pd.Grouper for second column beacuse its a string object and not a time object. pd.Grouper is only compatible with datetime columns.

5 Comments

is it "you don't have to" or "is only compatible"? could you clarify that? The former would imply that the current accepted answer is valid, the latter would imply that the current accepted answer is invalid.
I tried the accepted answer and it didn't work for me because, In my case (and also the case with the question posted here), the 'other_column' was not a date-time series. Instead it was a 'string' series as asked here. pd.Grouper only accepts a datetime series. If you give it something else, like a string series, it won't work and throw an exception. Hope that clarifies.
Thanks for the comment. It's weird because from the docs (pandas.pydata.org/docs/reference/api/pandas.Grouper.html) it does seems like Grouper can take the name of the column too.
what does foo mean after transform?
@GustavoZárate it's a placeholder for a function to feed into the GroupBy.transform method. transform will summarize values in the same group using the foo function BUT return the same result in a series with the same index (and thus, the same length and order) of the original DataFrame. More info in the docs: pandas.pydata.org/pandas-docs/stable/user_guide/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.