pythonpandasplotseaborncountplot

Countplot of multilabel data


How can I make countplot of multilabel data? I have a pandas dataset with a column 'Genre'; there may be more than one genre for a movie. I need one countplot with bars for different classes (something that seaboorn.countplot would do).

    Genre   
0   ['drama', 'comedy']
1   ['action']
2   ['drama']
3   ['action', 'comedy']

I tried seaborn countplot, but it seems that it doesn't work with lists of labels.


Solution

  • If data are nested lists then first flatten, convert to DataFrame and plot:

    import ast
    #if necessary convert data to lists form strings
    #df['Genre'] = df['Genre'].apply(ast.literal_eval)
    
    df = pd.DataFrame({'a':[y for x in df['Genre'] for y in x]})
    sns.countplot(x='a', data=df)