1

I have already completed the separation of the genres for movies now I want to get the number of movies under each each genre, following is my completed code for separating the genres

       df['geners_arr']=df['genres'].str.split('|')

       df.head()
       count_lambda=lambda x: len(x)
       df['Genre_count_for_Movie']=df.geners_arr.apply(count_lambda)
       df.head(3)`
Ddevil Prasad
  • 117
  • 1
  • 14

1 Answers1

1

I think your solution working nice if no NaNs.

More general is str.len:

df['Genre_count_for_Movie']=df.geners_arr.str.len()

Or count | and add 1:

df['Genre_count_for_Movie']= df['genres'].str.count('|') + 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252