0

Hi I did groupby and nlargest on a dataframe. The result is a series, is there anyway to keep the result as a dataframe ?

ID NO
0 10
0 20
0 30
1 20
1 30
1 40
2 30
3 40

My code TOP2 = df.groupby('ID')['NO'].nlargest(2) .

It returns correct result, but I would like it to be a dataframe, not series.

Osca
  • 1,588
  • 2
  • 20
  • 41
  • I usually coerce it back to a df.. Would be interested to know if there are other means too – kerwei Jan 24 '19 at 06:56
  • `TOP2 = pd.DataFrame(df.groupby('ID')['NO'].nlargest(2))` will make it a dataframe with one column. – Joe Patten Jan 24 '19 at 06:57
  • Use `df.groupby('ID', as_index=False)['NO'].nlargest(2)` or `df.groupby('ID')['NO'].nlargest(2).reset_index()`. – Space Impact Jan 24 '19 at 07:05
  • Scroll down to "Question 2" in the duplicate link [here](https://stackoverflow.com/questions/53781634/aggregation-in-pandas). – cs95 Jan 24 '19 at 07:09
  • @JoePatten . Thank you! – Osca Jan 24 '19 at 22:09
  • @SandeepKadapa Thanks, `df.groupby('ID', as_index=False)['NO'].nlargest(2)` is the most suitable solution. Just have once more question, if my dataframe has other columns `location` and `category` and I want to show it in the result as well. The result will be show top2 highest ID, the NO, ID location and ID category. How can I edit the code? – Osca Jan 24 '19 at 22:25

0 Answers0