Let it be the following python pandas dataframe:
| other | value | time_UK | total_time_UK | color | counter_time_UK |
|---|---|---|---|---|---|
| 392 | idk | 0 | 0 | red | 8393283 |
| 2393 | odp | 930233 | 23423423 | blue | 322332 |
| 392 | kfl | 0 | 0 | red | 0 |
| 323 | bcn | 783732 | 32423 | green | 42332 |
| 9695 | uie | 0 | 0 | red | 3423283 |
| 111 | xas | 0 | 3423323 | blue | 322 |
I would like for the columns containing the string time_UK, to convert their values in seconds to TimeDelta and if its value is 0 to NaT.
| other | value | time_UK | total_time_UK | color | counter_time_UK |
|---|---|---|---|---|---|
| 392 | idk | NaT | NaT | red | pd.to_timedelta(8393283) |
| 2393 | odp | pd.to_timedelta(930233) | pd.to_timedelta(23423423) | blue | pd.to_timedelta(322332) |
| 392 | kfl | NaT | NaT | red | NaT |
| 323 | bcn | pd.to_timedelta(783732) | pd.to_timedelta(32423) | green | pd.to_timedelta(42332) |
| 9695 | uie | NaT | NaT | red | pd.to_timedelta(3423283) |
| 111 | xas | NaT | pd.to_timedelta(3423323) | blue | pd.to_timedelta(322) |
Right now Im using next code:
# Sets the 0 second values to NaT
df.loc[df['time_UK'] == 0.0, 'time_UK'] = pd.NaT
# Converts seconds to TimeDelta type
df['time_UK'] = pd.to_timedelta(df['time_UK'], unit='s')
I am grateful for the help offered.