我有一个数据帧,格式如下,其值为%h%%S

我想,对于每一行,用值减1填充上面的一行,用值加1填充下一行,这样就会是这样的

等
我尝试过使用df.ffill的选项,但没有使用锁
发布于 2021-07-24 13:41:28
尝试:
df['date']=pd.to_datetime(df['date'],format='%H%M%S')
df=df.reindex(df.index.repeat(3)).reset_index(drop=True)
s1=(df.index%3)==0
s2=(df.index%3)==2
df.loc[s1,'date']=df.loc[s1,'date']-pd.DateOffset(hours=1)
df.loc[s2,'date']=df.loc[s2,'date']+pd.DateOffset(hours=1)
df['date']=df['date'].dt.strftime('%H%M%S')df的输出
col1 col2 date
0 X A 002230
1 X A 012230
2 X A 022230
3 Y B 131245
4 Y B 141245
5 Y B 151245
6 Z C 220130
7 Z C 230130
8 Z C 000130我使用的示例dateframe:
df=pd.DataFrame({'col1': {0: 'X', 1: 'Y', 2: 'Z'},
'col2': {0: 'A', 1: 'B', 2: 'C'},
'date': {0: '012230', 1: '141245', 2: '230130'}})https://stackoverflow.com/questions/68507203
复制相似问题