【发布时间】:2019-12-26 14:23:05
【问题描述】:
我的原始数据框包含来自多个实体的每小时数据。sample(5) of original dataframe
我使用 pd.pivot_table 和 index=pd.Grouper(freq='M') 来获取按月分组的每个实体的值的总和。
piv=pd.pivot_table(df, values='EnergyValue',index=pd.Grouper(freq='M', key='DDMM'),columns=['entityID'], aggfunc=np.sum)
我现在想用 plotly 来绘制它。 example of dataframe after pivot_table
我可以通过添加 .plot() 直接绘制数据透视表
pd.pivot_table(df, values='EnergyValue',index=pd.Grouper(freq='M', key='DDMM'),columns=['entityID'], aggfunc=np.sum).plot()
.plot() directly after pivot_table operation on dataframe
但是我希望这个图表在情节上。 我试图堆叠 pivot_table 然后绘图。 dataframe after stack operation. I want to plot the values column (red arrow) as y-value and index as x-value
但是,我无法将该值用作 y 轴 我如何访问这个 y 值?
stack=piv.stack()
px.line(stack,x='DDMM',y=piv.values,color='entityID')
【问题讨论】:
-
y参数目前只接受数据框参数的列名。 -
我明白了。感谢您澄清 px 仅采用列名。那我该怎么画呢?有没有办法使堆栈操作(piv.stack())的结果成为具有命名列的数据帧?
-
你的原始数据框已经是正确的格式,PX可以正常工作,实际上你不需要做任何堆叠。
-
你可以在没有数据透视表的情况下使用 groupby
-
非常感谢尼古拉斯。我在结果数据帧上使用了 groupby 然后 reset_index 。然后我将“DDMM”作为 x 值传递给 px。这行得通。非常感谢您的帮助。
标签: python-3.x pandas indexing pivot plotly