在 Seaborn 中使用 .count() Pandas 并分配给 y 轴 returns 空图表
Using .count() Pandas and assigning to y axis returns empty chart in Seaborn
我正在尝试对单词“pos”和“neg”出现的次数做一个简单的 .count()
,以便我可以将其应用到我在 Seaborn 上的条形图的 Y 轴。
这是我的代码:
df['score'] = df['compound'].apply(lambda x: 'pos' if x>= 0 else 'neg')
df['overall'] = df[(df.score == "pos") & (df.score == "neg")].count()
plt.figure(2)
chart_2 = sns.barplot(x='Category', y='overall', data=df)
当我 运行 这个时,plt.figure(2) return 是一个空图表。我试过 .sum()
没有用,而且 return 是一张空图表。
如果我执行此操作,它将 return 总计而不会在 x-ais 中按 Category
细分。例如,所有类别的总数为 58,这是数据帧总数。
df['overall'] = df['score'].count()
.value_counts()
也是 return 一个空图表。
我 运行 想不通为什么会这样!
提前致谢。
如评论中所述,(df.score == "pos") & (df.score == "neg")
通过 AND
关系组合,在所有情况下都会给出 False
。在 (df.score == "pos") | (df.score == "neg")
中使用 OR
将在所有情况下给出 True。但是,它不会区分类别,所以 df['overall']
到处都是 58。
创建计数条形图的最简单方法是 seaborn 的 sns.countplot()
。您可以设置 x='Category'
来计算每个类别。 hue='score'
将根据分数拆分。
要直接创建条形图,并通过 pandas 进行计数,您需要类似 df['overall'] = df['Category'].apply(lambda cat: (df['Category'] == cat).sum())
的东西。此处,(df['Category'] == cat)
创建一个布尔值数组 True
和 False
。当对这些调用 sum()
时,True
被认为是 1
,False
被认为是 0
,因此 sum()
将计算 [=22] 的数量=] 值。
Pandas' 按类别计数的首选方法是通过 groupby('Category')
然后取每个组的 size()
。
这是一个例子:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({'compound': [-20, -10, 100, 200, 300, -20, -10, 100, -10, 100, 200, 300],
'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c']})
df['score'] = df['compound'].apply(lambda x: 'pos' if x >= 0 else 'neg')
fig, (ax1, ax2, ax3, ax4) = plt.subplots(ncols=4, figsize=(14, 4))
sns.countplot(data=df, x='Category', hue='score', palette='rocket', ax=ax1)
ax1.set_title('Countplot: Score per category')
ax1.locator_params(axis='y', integer=True)
sns.countplot(data=df, x='Category', palette='turbo', ax=ax2)
ax2.set_title('Countplot: Overall sum per category')
df['overall'] = df['Category'].apply(lambda cat: (df['Category'] == cat).sum())
sns.barplot(data=df, x='Category', y='overall', palette='turbo', ax=ax3)
ax3.set_title('Barplot: Using the "overall" column')
df_counts = df.groupby('Category', as_index=False).size()
sns.barplot(data=df_counts, x='Category', y='size', palette='turbo', ax=ax4)
ax4.set_title('Barplot: Using groupby')
sns.despine()
plt.tight_layout()
plt.show()
我正在尝试对单词“pos”和“neg”出现的次数做一个简单的 .count()
,以便我可以将其应用到我在 Seaborn 上的条形图的 Y 轴。
这是我的代码:
df['score'] = df['compound'].apply(lambda x: 'pos' if x>= 0 else 'neg')
df['overall'] = df[(df.score == "pos") & (df.score == "neg")].count()
plt.figure(2)
chart_2 = sns.barplot(x='Category', y='overall', data=df)
当我 运行 这个时,plt.figure(2) return 是一个空图表。我试过 .sum()
没有用,而且 return 是一张空图表。
如果我执行此操作,它将 return 总计而不会在 x-ais 中按 Category
细分。例如,所有类别的总数为 58,这是数据帧总数。
df['overall'] = df['score'].count()
.value_counts()
也是 return 一个空图表。
我 运行 想不通为什么会这样!
提前致谢。
如评论中所述,(df.score == "pos") & (df.score == "neg")
通过 AND
关系组合,在所有情况下都会给出 False
。在 (df.score == "pos") | (df.score == "neg")
中使用 OR
将在所有情况下给出 True。但是,它不会区分类别,所以 df['overall']
到处都是 58。
创建计数条形图的最简单方法是 seaborn 的 sns.countplot()
。您可以设置 x='Category'
来计算每个类别。 hue='score'
将根据分数拆分。
要直接创建条形图,并通过 pandas 进行计数,您需要类似 df['overall'] = df['Category'].apply(lambda cat: (df['Category'] == cat).sum())
的东西。此处,(df['Category'] == cat)
创建一个布尔值数组 True
和 False
。当对这些调用 sum()
时,True
被认为是 1
,False
被认为是 0
,因此 sum()
将计算 [=22] 的数量=] 值。
Pandas' 按类别计数的首选方法是通过 groupby('Category')
然后取每个组的 size()
。
这是一个例子:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({'compound': [-20, -10, 100, 200, 300, -20, -10, 100, -10, 100, 200, 300],
'Category': ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c']})
df['score'] = df['compound'].apply(lambda x: 'pos' if x >= 0 else 'neg')
fig, (ax1, ax2, ax3, ax4) = plt.subplots(ncols=4, figsize=(14, 4))
sns.countplot(data=df, x='Category', hue='score', palette='rocket', ax=ax1)
ax1.set_title('Countplot: Score per category')
ax1.locator_params(axis='y', integer=True)
sns.countplot(data=df, x='Category', palette='turbo', ax=ax2)
ax2.set_title('Countplot: Overall sum per category')
df['overall'] = df['Category'].apply(lambda cat: (df['Category'] == cat).sum())
sns.barplot(data=df, x='Category', y='overall', palette='turbo', ax=ax3)
ax3.set_title('Barplot: Using the "overall" column')
df_counts = df.groupby('Category', as_index=False).size()
sns.barplot(data=df_counts, x='Category', y='size', palette='turbo', ax=ax4)
ax4.set_title('Barplot: Using groupby')
sns.despine()
plt.tight_layout()
plt.show()