Search code examples
pythonindexingpandas-groupbymulti-index

Multiindex groupby python


I have a Multiindex dataframe like this after groupby and sort value descending usage

peak = df.groupby(["month_year"]).apply(lambda x: x.sort_values(["Usage"], ascending = False)

                  DateTime	       Usage
month_year								
2012-01	  2055	2012-01-22 10:00:00	55	
          351	2012-01-04 16:00:00	52	
         .....
2012-12	 34545	2012-12-25 20:30:00	22
         34505	2012-12-25 10:30:00	21

How can I only keep just the index of first row of each month_year? In other word, I only want to keep '2055' and '34545'?


Solution

  • One way to do this is to use reset_index and groupby:

    df1.reset_index(level=1).groupby('month_year').first()
    

    Output:

                level_1             DateTime  Usage
    month_year                                     
    2012-01        2055  2012-01-22 10:00:00     55
    2012-12       34545  2012-12-25 20:30:00     22