如何创建具有多个值的特征的 seaborn violinplot?

How to create a seaborn violinplot of a feature that take more than one value?

我正在尝试通过创建小提琴图来分析和可视化电影数据集。此处相关的特征是电影的评级及其流派。我的问题是大多数电影都有不止一种类型(我还没有对它们进行编码)。让我们以下面的数据为例:

genres = [['action', 'drama', 'comedy'], ['comedy', 'drama'], ['action', 'sci-fi', 'comedy']]
rating = [6, 8, 3]

data = {'Rating': rating, 'Genres': genres}
df = pd.DataFrame(data=data)

那会得到:

|  |Rating|          Genres              |
|--|------|------------------------------|
|0 |  6   |['action', 'drama', 'comedy'] |
|1 |  8   |['comedy', 'drama']           |
|2 |  3   |['action', 'sci-fi', 'comedy']|

如果每部电影只有一种类型,我会这样做:

sns.violinplot(x='Genres', y='Rating', data=df)

在特征中可以有多个值的情况下,我如何才能完成同样的事情?任何想法都非常受欢迎!

我想你想explode你的列表栏:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

genres = [['action', 'drama', 'comedy'], ['comedy', 'drama'], ['action', 'sci-fi', 'comedy']]
rating = [6, 8, 3]

data = {'Rating': rating, 'Genres': genres}
df = pd.DataFrame(data=data)

df = df.explode("Genres")
sns.violinplot(x='Genres', y='Rating', data=df)
plt.show()

示例输出: