pandas - 将数据汇总到不同年份的一个点并将它们绘制在图表中
问题描述
我想总结不同年份的数据点。所以我想绘制一个不同年份的图表,并从一个指定的属性中总结一个图表,所以让我们来看看 2011 -2015 年的硬币
所以我选择作为我使用的数据框熊猫:
Summerize the data point (way 1)
testn = test.groupby(by=[test.index.year,test.index.month]).sum()
print(testn['SalesDateTime'])
print (type(testn))
这给了我一个错误。错误告诉我没有属性年或月?
enter code here
Summerize the data point (way 1)
testn = test.groupby(by=[test.index.year,test.index.month]).sum()
print(testn['SalesDateTime'])
#print (type(testn))
Summerize the data point (way 2)
nieuw = test.groupby(by=[test.index.month, test.index.year])
print(nieuw)
testn.plot("SalesDateTime","Coins",)
plt.show(testn)
解决方案
我认为没有DatetimeIndex
,所以需要to_datetime
:
test.index = pd.to_datetime(test.index)
testn = test.groupby(by=[test.index.year,test.index.month]).sum()
或者:
testn = (test.groupby(by=[test.index.year.rename('year'),
test.index.month.rename('month')]).sum().reset_index())
推荐阅读
- amazon-web-services - 错误:连接 ECONNREFUSED 3.15.138.43:8080 我需要 IAM AWS_ACCESS_KEY_ID 吗?
- reactjs - 在 React 中取消 Axios 获取请求
- javascript - 为什么我的排序脚本工作不一致?
- javascript - 当 node.js 以块的形式出现时修改数据
- python - 移除具有 kivy 条件的对象粒子
- java - 将 AWS CDK EmailConfigurationProperty 与 UserPool 关联
- flutter - 使用 .withOpacity 时颤动的阿拉伯字母未显示正确的颜色
- json - 第一次将 Java 对象转换为 JSON 字符串时的性能问题,第二次工作正常
- c - 为什么我在编写代码之前就收到错误消息?
- ruby-on-rails - 在 ruby on rails 6 中通过 gmail 帐户从 localhost 发送电子邮件