1 回答
TA贡献2041条经验 获得超4个赞
如果需要分次:
df['new'] = df['col1'].div(df.groupby(df.index.time)['col1'].transform('mean'))
print (df)
col1 new
DateTime
2008-04-28 09:40:00 300.0 0.75
2008-04-28 09:45:00 -800.0 -inf
2008-04-28 09:50:00 0.0 0.00
2008-04-28 09:55:00 -100.0 1.00
2008-04-28 10:00:00 0.0 NaN
2008-04-29 09:40:00 500.0 1.25
2008-04-29 09:45:00 800.0 inf
2008-04-29 09:50:00 100.0 2.00
2008-04-29 09:55:00 -100.0 1.00
2008-04-29 10:00:00 0.0 NaN
或者如果需要按天划分:
df['new'] = df['col1'].div(df.groupby(df.index.date)['col1'].transform('mean'))
print (df)
col1 new
DateTime
2008-04-28 09:40:00 300.0 -2.500000
2008-04-28 09:45:00 -800.0 6.666667
2008-04-28 09:50:00 0.0 -0.000000
2008-04-28 09:55:00 -100.0 0.833333
2008-04-28 10:00:00 0.0 -0.000000
2008-04-29 09:40:00 500.0 1.923077
2008-04-29 09:45:00 800.0 3.076923
2008-04-29 09:50:00 100.0 0.384615
2008-04-29 09:55:00 -100.0 -0.384615
2008-04-29 10:00:00 0.0 0.000000
添加回答
举报