'Get most frequent elements across all groups in a Panda time series

How one can plot the count of the n most frequent elements across all groups for a given multi group time series? Note this is different from n most frequent elements of each group, which could be accomplished with count and nlargest.

Given a dataframe:

import pandas as pd

data = {'year': [2020, 2020, 2021, 2021, 2022], 
        'month': [1, 1, 2, 2, 3],
        'Name': ['name_1', 'name_2', 'name_1', 'name_2', 'name_1'], 
        'count': [10, 12, 8, 10, 2]}  

df = pd.DataFrame(data)

print(df)

which outputs

   year  month    Name  Count
0  2020      1  name_1     10
1  2020      1  name_2     12
2  2021      2  name_1      8
3  2021      2  name_2     10
4  2022      3  name_1      2

  • data should be grouped by year and month
  • I want n = 1, in other words the most frequent one

I would like to plot only name_1's count since, although it does not have the largest count in any group (or even overall), it "appears" more times.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source