0

I want to get all the top (max) months base on the count of each month in one column:

month

Oct
Oct
Oct
Jan
Jan
Jan
Feb
Feb
Aug

in this case, Oct and Jan are both top months with 3 counts.

With below code, I can only get one of the top months. How can I get the output for all the top months (Oct and Jan) instead of only Oct or Jan? The problem is I am not sure how to detect how many months with same max count.

top_month = df.groupby('month')['month'].count().sort_values().tail(1).index.values[0]

This output I want is:

Top month: Oct, Jan
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • You are looking for [`mode()`](https://pandas.pydata.org/docs/reference/api/pandas.Series.mode.html#pandas.Series.mode)... – Tomerikoo Mar 25 '21 at 07:29
  • with mode(), I got something like this ``` 0 Oct 1 Jan ``` how can I I just get Oct and Jan? – Mark Liu Mar 25 '21 at 08:58
  • Did you read the documentation? `mode` returns a `Series`. You can always just do `.mode().tolist()` – Tomerikoo Mar 25 '21 at 09:02

0 Answers0