![](/img/trans.png)
[英]How to plot density plot by label (categorical variable) for each numeric column?
[英]count plot for each categorical variable
您可以在列上使用value_counts
,然后繪制:
# grouped by quartile
df.apply(pd.Series.value_counts).T.plot.bar()
# grouped by value
df.apply(pd.Series.value_counts).plot.bar()
僅使用 Pandas 的一種快速方法是:
df.groupby(level=0, axis=1).plot.bar()
但這對於顏色/布局來說並不靈活。
如果您想要一個不錯的輸出,請將您的數據seaborn.catplot
重新處理為長格式並使用seaborn.catplot
:
import seaborn as sns
df2 = (df.unstack()
.reset_index()
.rename(columns={'level_0': 'quartile',
'level_1': 'index',
0: 'value'})
)
sns.catplot(data=df2, col='quartile', x='index', y='value', kind='bar')
我使用了以下輸入:
Q1 Q2 Q3
0 4 1 5
1 1 5 1
2 1 2 1
3 1 4 1
4 5 1 5
import seaborn as sns
df2 = (df.apply(pd.Series.value_counts)
.unstack()
.reset_index()
.rename(columns={'level_0': 'quartile',
'level_1': 'index',
0: 'counts'})
)
sns.catplot(data=df2, col='quartile', x='index', y='counts', kind='bar')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.