我有一组从07-2月19日到17-2月19日的小时数据:
t v_amm v_alc v_no2
0 2019-02-07 08:00:00+00:00 0.320000 0.344000 1.612000
1 2019-02-07 09:00:00+00:00 0.322889 0.391778 1.580889
2 2019-02-07 10:00:00+00:00 0.209375 0.325208 2.371250
...
251 2019-02-17 19:00:00+00:00 1.082041 0.652041 0.967143
252 2019-02-17 20:00:00+00:00 0.936923 0.598654 1.048077
253 2019-02-17 21:00:00+00:00 0.652553 0.499574 1.184894
另一组类似的小时数据,从01-3月19日到11-3月-19:
t v_amm v_alc v_no2
0 2019-03-01 00:00:00+00:00 0.428222 0.384444 1.288222
1 2019-03-01 01:00:00+00:00 0.398600 0.359600 1.325800
2 2019-03-01 02:00:00+00:00 0.365682 0.352273 1.360000
...
244 2019-03-11 04:00:00+00:00 0.444048 0.415238 1.265000
245 2019-03-11 05:00:00+00:00 0.590698 0.591395 1.156977
246 2019-03-11 06:00:00+00:00 0.497872 0.465319 1.228298
然而,在17-Feb-19和01-3月-19之间没有可用的数据.因此,我想在缺失的日期和时间中插入以下数据(按周中的一天和小时分组):
v_amm v_alc v_no2
day_of_week hour
0 0 0.432222 0.351111 1.258889
1 0.371026 0.324359 1.323333
2 0.371026 0.324359 1.323333
3 0.250000 0.285000 1.510000
4 0.220000 0.274500 1.616500
5 0.195263 0.264211 1.666053
...
6 18 0.919158 0.557793 1.018703
19 1.065220 0.599320 0.965771
20 0.896227 0.543689 1.045634
21 0.648488 0.469210 1.187928
22 0.592200 0.417200 1.154400
23 0.485918 0.366531 1.215918
有人知道如何在熊猫身上获得这个吗?
发布于 2020-03-19 21:34:43
首先,生成缺少的索引,然后连接dataframes。
new_index = pd.date_range(start='2019-02-17', end='2019-03-01', freq='H')
new_df = pd.DataFrame([new_index], index=['t']).T
new_df['day_of_week'] = [z.weekday() for z in new_index]
new_df['hour'] = [z.hour for z in new_index]
new_df = new_df.merge(<your_df>, on=['day_of_week', 'hour']), how='left')
new_df = new_df.drop(['day_of_week', 'hour'], axis=1)
filled_df = pd.concat([<df1>, new_df, <df2>], axis=0)
https://stackoverflow.com/questions/60753659
复制相似问题