How could I count all the genres in my DataFrame?

Viewed 643

I have a DataFrame called df_imdb:

enter image description here

Each row contains the information about a movie, This DataFrame has a column name 'genres' that shows the genre of that movie that could have more than one genre e.g. [{'id': 53, 'name': 'Thriller'}, {'id': 28, 'name': 'Action'}, {'id': 9648, 'name': 'Mystery'}]

I want to find out what are the most genre used in this movies (find the top 3 most used genres in this DataFrame)

2 Answers

The data is a list of dictionaries, multiple options here:

Option 1: Pure pandas, convert the values associated with key name to a Series and use value_counts

df = pd.DataFrame({'genres':[[{'id': 53, 'name': 'Thriller'}, {'id': 28, 'name': 'Action'}, {'id': 9648, 'name': 'Mystery'}],[{'id': 53, 'name': 'Thriller'}, {'id': 30, 'name': 'Blah'}, {'id': 9648, 'name': 'Mystery'}]]})

df['genres'].apply(lambda x: pd.Series([i['name'] for i in x]))\
.stack().value_counts()

You get

Thriller    2
Mystery     2
Action      1
Blah        1

Option 2: Convert the values to list and use Counter

from collections import Counter
l_genres = df['genres'].apply(lambda x: [i['name'] for i in x]).sum()
Counter(l_genres)

You get

Counter({'Thriller': 2, 'Action': 1, 'Mystery': 2, 'Blah': 1})

df['genres'].apply(lambda x: pd.Series([i['name'] for i in x])).stack().value_counts()

Edit: Data type is str and not list, first use literal_eval

from ast import literal_eval
df['genres'] = df['genres'].apply(literal_eval)

Give this a try:

c = df_imbd['genres'].apply(lambda x: [n['name'] for n in x]).sum()
pd.Series(c).value_counts()
Related