2

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
diplodocus
  • 67
  • 1
  • 1
  • 7

1 Answers1

2

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)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252