【发布时间】:2022-01-08 18:58:42
【问题描述】:
对于下面的代码,我得到了一个意想不到的结果。
for j in range(len(bc)):
df.loc[df['dd'] == 0, 'time'] = datetime(2021,7,11,df.iloc[j]['hh'],df.iloc[j]['mm'],df.iloc[j]['ss'])
df.loc[df['dd'] == 1, 'time'] = datetime(2021,7,12,df.iloc[j]['hh'],df.iloc[j]['mm'],df.iloc[j]['ss'])
print(df.head(5))
我得到了什么
index dd hh mm ss time
0 0 days 04:52:00 0 4 52 0 2021-07-11 04:56:00
1 1 days 04:53:00 1 4 53 0 2021-07-11 04:56:00
2 0 days 04:54:00 0 4 54 0 2021-07-11 04:56:00
3 1 days 04:55:00 1 4 55 0 2021-07-11 04:56:00
4 0 days 04:56:00 0 4 56 0 2021-07-11 04:56:00
我想要什么
index dd hh mm ss time
0 0 days 04:52:00 0 4 52 0 2021-07-11 04:52:00
1 1 days 04:53:00 1 4 53 0 2021-07-12 04:53:00
2 0 days 04:54:00 0 4 54 0 2021-07-11 04:54:00
3 1 days 04:55:00 1 4 55 0 2021-07-12 04:55:00
4 0 days 04:56:00 0 4 56 0 2021-07-11 04:56:00
【问题讨论】:
标签: python pandas datetime for-loop