Attempting to find the 5 largest values per month using groupby

后端 未结 2 872
悲&欢浪女
悲&欢浪女 2021-01-25 13:56

I am attempting to show the top three values of nc_type for each month. I tried using n_largest but that doesn\'t do it by date.

Original Data:

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-25 14:28

    I'd include group_keys=False

    df.groupby('occurred_date', group_keys=False).nlargest(3)
    
    occurred_date  nc_type
    1.0            f          34
                   w          24
                   z          13
    12.0           w          44
                   g          42
                   a          27
    Name: value, dtype: int64
    

提交回复
热议问题