pandas groupby总和面积图
pandas groupby sum area plot
我希望根据 groupby 和 sum 创建的汇总数据随时间绘制堆积面积图。
groupby 和 sum 部分正确地对我想要的数据进行了分组和求和,但就绘图而言,结果格式似乎是无稽之谈。
我不确定从这里到哪里去:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.DataFrame({'invoice':[1,2,3,4,5,6],'year':[2016,2016,2017,2017,2017,2017],'part':['widget','wonka','widget','wonka','wonka','wonka'],'dollars':[10,20,30,10,10,10]})
#drop the invoice number from the data since we don't need it
df=df[['dollars','part','year']]
#group by year and part, and add them up
df=df.groupby(['year','part']).sum()
#plotting this is nonsense:
df.plot.area()
plt.show()
绘制多个系列,最简单的方法是将每个系列组织为一个单独的列,即替换
df=df.groupby(['year','part']).sum()
和
df=df.groupby(['year', 'part']).sum().unstack(-1)
那么其余的代码应该可以工作了。但是,我不确定这是否是您需要的,因为没有显示所需的输出。
df.plot.area()
然后生成类似
的图表
我希望根据 groupby 和 sum 创建的汇总数据随时间绘制堆积面积图。
groupby 和 sum 部分正确地对我想要的数据进行了分组和求和,但就绘图而言,结果格式似乎是无稽之谈。
我不确定从这里到哪里去:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.DataFrame({'invoice':[1,2,3,4,5,6],'year':[2016,2016,2017,2017,2017,2017],'part':['widget','wonka','widget','wonka','wonka','wonka'],'dollars':[10,20,30,10,10,10]})
#drop the invoice number from the data since we don't need it
df=df[['dollars','part','year']]
#group by year and part, and add them up
df=df.groupby(['year','part']).sum()
#plotting this is nonsense:
df.plot.area()
plt.show()
绘制多个系列,最简单的方法是将每个系列组织为一个单独的列,即替换
df=df.groupby(['year','part']).sum()
和
df=df.groupby(['year', 'part']).sum().unstack(-1)
那么其余的代码应该可以工作了。但是,我不确定这是否是您需要的,因为没有显示所需的输出。
df.plot.area()
然后生成类似