0

I have a dataframe that have dates (2011-03-12). I managed to split it into two additional columns year and month. Here reduced example:

df = pd.DataFrame([
    [2010, 1, 4], [2010, 1, 8], [2010, 2, 4], 
    [2011, 3, 8], [2010, 6, 16]],
    columns=['year', 'month', 'value'])

If I do:

df.groupby(['year', 'month']).sum()

I can clearly see that year 2010 has three months, but only 1 for 2011.

I could like to display how many months are represented each year.

year  months
2010       3
2011       1

How can I achieve that?

nowox
  • 25,978
  • 39
  • 143
  • 293

1 Answers1

2

TRy with nunique:

df = df.groupby('year').agg({'month': 'nunique'})

OUTPUT:

      month
year       
2010      3
2011      1
Nk03
  • 14,699
  • 2
  • 8
  • 22