For the code below, I am getting an unexpected result.
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))
What I get
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
What I want
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
CodePudding user response:
You could do:
df['time'] = df.apply(
lambda row: datetime(
2021, 7, 11 if row['dd'] == 0 else 12, row['hh'], row['mm'], row['ss']
),
axis=1
)