将数据汇总到不同年份的某一点并将其绘制在图表中

问题描述 投票:0回答:1

我想总结一下不同年份的数据。所以我想绘制一个不同年份的图表,并从指定属性总结一个图表,所以让我们在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)
pandas data-science
1个回答
0
投票

我认为没有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())
© www.soinside.com 2019 - 2024. All rights reserved.