-1

this is a code i wrote, but the output is too big, over 6000, how do i get the first result for each year

df_year = df.groupby('release_year')['genres'].value_counts()

chika
  • 21
  • 4
  • Have you checked [this question](https://stackoverflow.com/questions/20067636/pandas-dataframe-get-first-row-of-each-group)? – Ersel Er Jul 26 '22 at 06:37

1 Answers1

0

Let's start from a small correction concerning variable name: value_counts returns a Series (not DataFrame), so you should not use name starting from df.

Assume that the variable holding this Series is gen.

Then, one of possible solutions is:

result = gen.groupby(level=0).apply(lambda grp:
    grp.droplevel(0).sort_values(ascending=False).head(1))

Initially you wrote that you wanted the most popular genre in each year, so I sorted each group in descending order and returned the first row from the current group.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41