question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'mode' not recognized by df.groupby().agg(), but pd.Series.mode works

See original GitHub issue

This 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:open
  • Created 8 years ago
  • Reactions:9
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
kernccommented, Apr 7, 2017

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.

5reactions
gosuto-inzasherucommented, Oct 9, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found