将PANDAS DataFrame的索引重置为在同一图上覆盖单独的年份



我正在尝试在彼此之间绘制两组信息,其中数据分开了大约一年。

我正在使用以下代码:

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

Date            Person  Pts
Jan 5 2015 2pm  John    10  
Jan 8 2015 4pm  John    22
Jan 5 2015 3pm  Frank   4
Jan 7 2015 10pm Frank   8

df2

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

所需的输出:

df1

Index Person  Pts
0     John    10  
1     John    22
0     Frank   4
1     Frank   8

df2

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()

最新更新