0

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'?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.