這是我的資料框
consumption hour
start_time
2022-09-30 14:00:00 02:00 199.0 14.0
2022-09-30 15:00:00 02:00 173.0 15.0
2022-09-30 16:00:00 02:00 173.0 16.0
2022-09-30 17:00:00 02:00 156.0 17.0
2022-09-30 18:00:00 02:00 142.0 18.0
2022-09-30 19:00:00 02:00 163.0 19.0
2022-09-30 20:00:00 02:00 138.0 20.0
2022-09-30 21:00:00 02:00 183.0 21.0
2022-09-30 22:00:00 02:00 138.0 22.0
2022-09-30 23:00:00 02:00 143.0 23.0
我想這樣出去
consumption hour
start_time
2022-09-30 14:00:00 02:00 199.0 14.0
2022-09-30 15:00:00 02:00 173.0 15.0
2022-09-30 16:00:00 02:00 173.0 16.0
2022-09-30 17:00:00 02:00 156.0 17.0
2022-09-30 18:00:00 02:00 142.0 18.0
2022-09-30 19:00:00 02:00 163.0 19.0
2022-09-30 20:00:00 02:00 138.0 20.0
2022-09-30 21:00:00 02:00 183.0 21.0
2022-09-30 22:00:00 02:00 138.0 22.0
2022-09-30 23:00:00 02:00 143.0 23.0
*2022-09-31 00:00:00 02:00 00.0 00.0*
*2022-09-31 01:00:00 02:00 00.0 01.0*
這里我的索引是 datetime (start_time),我想創建具有 datetime 延續和值的行作為虛擬或零。如何在熊貓 python 中做到這一點?
uj5u.com熱心網友回復:
創建助手 DataFrame 并通過以下方式添加到原始資料中concat:
N = 2
df1 = (pd.DataFrame({'consumption':0},
index=pd.date_range(df.index.max() pd.Timedelta('1h'),
df.index.max() pd.Timedelta(f'{N}h'),
freq='H'))
.assign(hour=lambda x: x.index.hour))
df = pd.concat([df, df1])
print (df)
consumption hour
2022-09-30 14:00:00 02:00 199.0 14.0
2022-09-30 15:00:00 02:00 173.0 15.0
2022-09-30 16:00:00 02:00 173.0 16.0
2022-09-30 17:00:00 02:00 156.0 17.0
2022-09-30 18:00:00 02:00 142.0 18.0
2022-09-30 19:00:00 02:00 163.0 19.0
2022-09-30 20:00:00 02:00 138.0 20.0
2022-09-30 21:00:00 02:00 183.0 21.0
2022-09-30 22:00:00 02:00 138.0 22.0
2022-09-30 23:00:00 02:00 143.0 23.0
2022-10-01 00:00:00 02:00 0.0 0.0
2022-10-01 01:00:00 02:00 0.0 1.0
或DataFrame.reindex與增加N小時數的新索引一起使用:
N = 2
df = (df.reindex(pd.date_range(df.index.min(),
df.index.max() pd.Timedelta(f'{N}h'),
freq='H'), fill_value=0)
.assign(hour=lambda x: x.index.hour))
print (df)
consumption hour
2022-09-30 14:00:00 02:00 199.0 14
2022-09-30 15:00:00 02:00 173.0 15
2022-09-30 16:00:00 02:00 173.0 16
2022-09-30 17:00:00 02:00 156.0 17
2022-09-30 18:00:00 02:00 142.0 18
2022-09-30 19:00:00 02:00 163.0 19
2022-09-30 20:00:00 02:00 138.0 20
2022-09-30 21:00:00 02:00 183.0 21
2022-09-30 22:00:00 02:00 138.0 22
2022-09-30 23:00:00 02:00 143.0 23
2022-10-01 00:00:00 02:00 0.0 0
2022-10-01 01:00:00 02:00 0.0 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/537891.html
