'Pandas visualize top 3 in the top 3 categories

I have a dataset like this: (basically a movie database)

title   Genre   ratinglevel userid  user_rating_score White Chicks  PG-13   crude and sexual humor, language and some drug content  80  82 Lucky Number Slevin  R   strong violence, sexual content and adult language  100 82 Grey's Anatomy   TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  98 Prison Break TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  98 How I Met Your Mother    TV-PG   Parental guidance suggested. May not be suitable for all children.  70  94 Supernatural TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  95 Breaking Bad TV-MA   For mature audiences.  May not be suitable for children 17 and under.   110 97 The Vampire Diaries  TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  91 The Walking Dead TV-MA   For mature audiences.  May not be suitable for children 17 and under.   110 98 Pretty Little Liars  TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  96 Once Upon a Time TV-PG   Parental guidance suggested. May not be suitable for all children.  70  98 Sherlock TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  95 Death Note   TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  77 Naruto   TV-PG   Parental guidance suggested. May not be suitable for all children.  70  88 The Hunter   R   language and brief violence 100 88 Lottie Dottie Chicken    TV-Y    Suitable for all ages.  10  88 Arrow    TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  96 Black Mirror TV-MA   For mature audiences.  May not be suitable for children 17 and under.   110 80 The Originals    TV-14   Parents strongly cautioned. May be unsuitable for children ages 14 and under.   90  74 The 100  NR  This movie has not been rated.  124 97 Masha and the Bear   TV-Y    Suitable for all ages.  10  81 Ó Pai, Ó - Look at This  TV-MA   For mature audiences.  May not be suitable for children 17 and under.   110 81 Nymphomaniac: Volume 1   NR  This movie has not been rated.  124 81

I wish to plot/visualize which Genre's are watched more more. Which I am doing as below and it is fine.

import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(20,10))
df = df[['Genre', 'userid']].drop_duplicates()['Genre']
g = sns.countplot(y = df, order=df.value_counts().index[:10])
plt.title("Top 10 Genre")
plt.show()

I wish to somehow add three most watched title in each genre in the same plot or connected plot. I don't have a attempt on this as not able to visualize the best way to do visualization.



Solution 1:[1]

Groupby I think is your friend here. dt.Groupby(['Genre', 'Movie'])['userid'].count() or something like it (adapted to your dataset) should group by genre and movie then count the number of userid under each group. Reset the index to convert the data back to a DataFrame and you can sort/filter all you want to get your answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan