I have a df like that:
| month | stock | MV |
|---|---|---|
| 1994-07 | A | 50 |
| 1994-07 | B | 60 |
| 1994-07 | C | 70 |
| 1994-07 | D | 80 |
| 1994-08 | A | 90 |
| 1994-08 | B | 60 |
| 1994-08 | C | 70 |
| 1994-08 | D | 95 |
| 1994-08 | E | 100 |
| 1994-08 | F | 110 |
I would like to subset my df in a way that I only have in it the 50% of the highest MV per month. For July/1994 I only have 4 stock, so 50% will be the 2 highest MV. For the month after, I have 6 stocks, which gives me 3 highest values:
| month | stock | MV |
|---|---|---|
| 1994-07 | C | 70 |
| 1994-07 | D | 80 |
| 1994-08 | D | 95 |
| 1994-08 | E | 100 |
| 1994-08 | F | 110 |
I have tried:
df = df.groupby(pd.Grouper(freq="M")).nlargest(2, "MV")
But I got the error: AttributeError: 'DataFrameGroupBy' object has no attribute 'nlargest' In addition, the value of n will need to be a different value for every month. I am not sure how to handle that as well.