我有個問題。我想列印分組條形圖(使用 seaborn 或 matplotlib)。當我運行下面的代碼片段時,我得到了
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
d = {'type': ['House', 'Boat', 'Bus', 'Bus', 'Boat'], 'room': ['private', 'public', 'public', 'private', 'nothing']}
df = pd.DataFrame(data=d)
print(df)
[OUT]
type room
0 House private
1 Boat public
2 Bus public
3 Bus private
4 Boat nothing
g = sns.catplot(
data=df, kind="bar",
x='type', y=df.groupby(['type']).count(), hue="room",
palette="dark", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("", "Counts")
g.legend.set_title("")
[OUT]
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我想要的是

uj5u.com熱心網友回復:
看起來這可以解決問題:
pd.crosstab(df['type'],df['room']).plot.bar(stacked=True)

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349862.html
標籤:Python 熊猫 数据框 matplotlib 海生
上一篇:使用多索引更改資料框的形狀
