Python.sum()给了我一个不同的结果扁钢()和单色条形图(),哪个是正确的?

2024-05-20 06:18:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,看起来像:

User ID | Group | Revenue
101     |   1   |    0
102     |   2   |    1.3
103     |   2   |    0.5
103     |   1   |    2.3
104     |   1   |    1.4
...     |   ... |   ...

我想知道每个团体的收入。我已经做到了:

df.groupby('Group').Revenue.sum()

已返回:

Group
Group 1    643.00
Group 2    351.47
Name: Revenue, dtype: float64

然后,我在matplotlib中通过执行以下操作绘制了此图:

plt.bar(df.Group, df.Revenue)
plt.show()

我得到了这个图表:

enter image description here 第一组在左边。你知道吗

在seaborn身上试用,我写了以下代码:

sns.barplot(data=df, x='Group', y='Revenue')
plt.show()

我得到了这个图表:

enter image description here 第一组在右边

我希望得到每个组的总收入,但不确定哪个结果是正确的,以及其他人试图显示什么。不知道如何改变图表,如果他们是错误的,以显示真正的总收入。你知道吗

谢谢。你知道吗


Tags: 数据nameiddfshow图表groupplt
1条回答
网友
1楼 · 发布于 2024-05-20 06:18:53

完成分组依据后,可以使用该分组依据对象绘制条形图:

In [8]: df = pd.DataFrame([[1, 343], [1, 300], [2, 300], [2, 51.47]], columns=['Group', 'Revenue'])

In [9]: df.groupby('Group').Revenue.sum().plot.bar()
Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0x7f97345e7710>

In [10]: plt.show()

相关问题 更多 >