首页 > 解决方案 > 将数据汇总到不同年份的一个点并将它们绘制在图表中

问题描述

我想总结不同年份的数据点。所以我想绘制一个不同年份的图表,并从一个指定的属性中总结一个图表,所以让我们来看看 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)

标签: pandasdata-science

解决方案


我认为没有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())

推荐阅读