我正在使用以下代碼繪制條形圖:
df.groupby(['Borrow_Rank'])['Outcome'].mean().plot(kind = 'bar')
這顯示了范圍從 0 到 1 的結果變數的平均值。但是,我還需要條形圖來顯示每組中有多少實體或行(每組 2000-5000)。我很欣賞任何想法。
先感謝您!
uj5u.com熱心網友回復:
您希望如何顯示資料尚不清楚,但您可以使用幾個聚合函式:
df.groupby(['Borrow_Rank'])['Outcome'].agg(['mean', 'count']).plot.bar()
輸出:

其他選項,將計數注釋為文本:
g = df.groupby(['Borrow_Rank'])['Outcome']
s = g.mean()
ax = s.plot.bar()
for x, (y, count) in enumerate(zip(s, g.count())):
ax.annotate(f'n = {count}', (x, y), ha='center', va='bottom')
輸出:

可重現的虛擬輸入:
df = pd.DataFrame({'Borrow_Rank': list('AABABACC'), 'Outcome': range(8)})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495114.html
標籤:Python 熊猫 数据框 matplotlib 条形图
上一篇:切線在圖形上的影片移動
