Python 重置数据框的索引以在同一图表上覆盖不同的年份
我试图将两组信息放在一起,数据间隔大约一年 我正在使用以下代码:Python 重置数据框的索引以在同一图表上覆盖不同的年份,python,pandas,dataframe,matplotlib,indexing,Python,Pandas,Dataframe,Matplotlib,Indexing,我试图将两组信息放在一起,数据间隔大约一年 我正在使用以下代码: ax = df1.loc[df1['Person']==John,['Pts']].expanding().mean().plot() df2.loc[df2['Person']==John,['Pts']].expanding().mean().plot(ax=ax) plt.show() 问题:我得到了一张图,显示了图中两个不同部分的扩展平均值,因为观测值相隔大约一年 目标:我希望它们相互重叠 当前情况:两个数据帧由da
ax = df1.loc[df1['Person']==John,['Pts']].expanding().mean().plot()
df2.loc[df2['Person']==John,['Pts']].expanding().mean().plot(ax=ax)
plt.show()
问题:我得到了一张图,显示了图中两个不同部分的扩展平均值,因为观测值相隔大约一年
目标:我希望它们相互重叠
当前情况:两个数据帧由datetime变量“Date”索引
df1
df2
期望输出:
df1
df2
欢迎大家提出意见,谢谢 我找到了解决问题的方法,以防其他人遇到类似问题:
grouped1 = df1.groupby('Person', as_index=False)
grouped2 = df2.groupby('Person', as_index=False)
john1 = grouped1.get_group('John').reset_index()
john2 = grouped2.get_group('John').reset_index()
ax = john1['Pts'].expanding().mean().plot()
john2['Pts'].expanding().mean().plot(ax=ax)
plt.show()
Date Person Pts
Jan 3 2016 1pm John 15
Jan 9 2016 5pm John 30
Jan 4 2016 2pm Frank 12
Jan 8 2016 9pm Frank 15
Index Person Pts
0 John 10
1 John 22
0 Frank 4
1 Frank 8
Date Person Pts
0 John 15
1 John 30
0 Frank 12
1 Frank 15
grouped1 = df1.groupby('Person', as_index=False)
grouped2 = df2.groupby('Person', as_index=False)
john1 = grouped1.get_group('John').reset_index()
john2 = grouped2.get_group('John').reset_index()
ax = john1['Pts'].expanding().mean().plot()
john2['Pts'].expanding().mean().plot(ax=ax)
plt.show()