0

i can use mean and median with groupby with this line:

newdf.groupby('dropoff_site')['load_weight'].mean()
newdf.groupby('dropoff_site')['load_weight'].median()

But when i use it for mode like this:

newdf.groupby('dropoff_site')['load_weight'].mode()

An error popped up, saying: 'SeriesGroupBy' object has no attribute 'mode'

What should i do?

update: from GroupBy pandas DataFrame and select most common value i used

source2.groupby(['Country','City'])['Short name'].agg(pd.Series.mode)

as

newdf.groupby(['dropoff_site'])['load_weight'].agg(pd.Series.mode)

because this has multimodal, but now the error goes: Must produce aggregated value

  • This is because `mode` does not necessarily produce a unique value (there can be several modes) – mozway Oct 13 '22 at 12:07

1 Answers1

1

Try this...

newdf.groupby('dropoff_site')['load_weight'].agg(pd.Series.mode)
Sachin Kohli
  • 1,956
  • 1
  • 1
  • 6