'mode' not recognized by df.groupby().agg(), but pd.Series.mode works
See original GitHub issueThis works:
df = pd.DataFrame({'A': [1, 2, 1, 2, 1, 2, 3], 'B': [1, 1, 1, 2, 2, 2, 2]})
df.groupby('B').agg(pd.Series.mode)
but this doesn’t:
df.groupby('B').agg('mode')
...
AttributeError: Cannot access callable attribute 'mode' of 'DataFrameGroupBy' objects, try using the 'apply' method
I thought all the series aggregate methods propagated automatically to groupby, but I’ve probably misunderstood?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:9
- Comments:6 (4 by maintainers)
Top Results From Across the Web
GroupBy pandas DataFrame and select most common value
The useful thing about Series.mode is that it always returns a Series, making it very compatible with agg and apply , especially when...
Read more >Comprehensive Guide to Grouping and Aggregating with ...
Pandas groupby and aggregation provide powerful capabilities for summarizing data. This article will discuss basic functionality as well as ...
Read more >pandas.DataFrame.squeeze — pandas 1.5.2 documentation
This method is most useful when you don't know if your object is a Series or DataFrame, but you do know it has...
Read more >Pandas GroupBy: Group, Summarize, and Aggregate Data in ...
The Pandas .groupby() method works in a very similar way to the SQL GROUP BY ... They're not simply repackaged, but rather represent...
Read more >GroupBy in SQL & Python: Comparison for Data Analysis | Mode
But there are actually many overlapping functions that can be ... Unlike SQL, the pandas groupby() method does not have a concept of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What about when grouping Series?
I have no issue with
.agg('mode')
returning the first mode, if any, while issuing a warning if the modes were multuple.I encountered this problem and ended up settling for this:
.agg({'column': lambda x: pd.Series.mode(x)[0][0]})
But yes, I agree with @kernc, I would not mind
.agg('mode')
returning the first mode if multiple modes are returned.