按组的绘图平均值,每个栏中指定数量的实例

发布于 2025-02-09 13:52:00 字数 185 浏览 0 评论 0原文

我正在使用以下代码来绘制条形图:

df.groupby(['Borrow_Rank'])['Outcome'].mean().plot(kind = 'bar')

这显示了从0-1范围内的结果变量的平均值。但是,我需要条形图还可以显示每个组中有多少个实例或行(每个组中的2000-5000)。

I am using the following code to plot a bar chart:

df.groupby(['Borrow_Rank'])['Outcome'].mean().plot(kind = 'bar')

This is showing the mean of the Outcome variable that ranges from 0-1. However, I need the bar chart to also show how many instances or rows are in each group (2000-5000 in each group).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2025-02-16 13:52:00

您想要显示的数据尚不清楚,但是您可以使用多个聚合功能:

df.groupby(['Borrow_Rank'])['Outcome'].agg(['mean', 'count']).plot.bar()

output:

“在此处输入图像说明”

其他选项,将计数注释为文本:

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)})

How you want the data displayed is unclear, but you could use several aggregation functions:

df.groupby(['Borrow_Rank'])['Outcome'].agg(['mean', 'count']).plot.bar()

output:

enter image description here

Other option, annotate the counts as text:

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')

output:

enter image description here

reproducible dummy input:

df = pd.DataFrame({'Borrow_Rank': list('AABABACC'), 'Outcome': range(8)})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文