这可能很容易,但由于某种原因,我发现它很难完成。任何提示将非常感谢。我有一些每天 5 分钟间隔的时间序列数据,唉:
Date Values
2012-12-05 09:30:00 5
2012-12-05 09:35:00 7
2012-12-05 09:40:00 3
2012-12-05 09:45:00 2
2012-12-05 09:50:00 15
2012-12-06 09:30:00 4
2012-12-06 09:35:00 3
2012-12-06 09:40:00 8
2012-12-06 09:45:00 1
我想计算相对于当天第一个值的差异(在本例中始终是 9:30 值),即。最终得到这个数据框:
Date Values
2012-12-05 09:30:00 0
2012-12-05 09:35:00 2
2012-12-05 09:40:00 -2
2012-12-05 09:45:00 -3
2012-12-05 09:50:00 10
2012-12-06 09:30:00 0
2012-12-06 09:35:00 -1
2012-12-06 09:40:00 4
2012-12-06 09:45:00 -3
你需要减去Series
已创建transform http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.transform.html with groupby http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html by Series.dt.date http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.date.html and first http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.first.html:
print (df.Values.groupby(df.Date.dt.day).transform('first'))
0 5
1 5
2 5
3 5
4 5
5 4
6 4
7 4
8 4
Name: Values, dtype: int64
df.Values = df.Values - df.Values.groupby(df.Date.dt.day).transform('first')
print (df)
Date Values
0 2012-12-05 09:30:00 0
1 2012-12-05 09:35:00 2
2 2012-12-05 09:40:00 -2
3 2012-12-05 09:45:00 -3
4 2012-12-05 09:50:00 10
5 2012-12-06 09:30:00 0
6 2012-12-06 09:35:00 -1
7 2012-12-06 09:40:00 4
8 2012-12-06 09:45:00 -3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)