-1

I have an Hour column from which I want to fetch only the hour values and put them in another column in the data frame.

The data looks like this, Hour 09:01:00 09:20:00 09:35:00 09:48:00

What I want is 09:01:00 - 9(new_value) 09:20:00 - 9(new_value) 09:35:00. - 10(new_value) 09:48:00 - 10(new_value)

since the time is 09:35:00, I want to count it as 10 hours because it has crossed the half-hour mark.

dt.hour gives,

new_values 9 9 9 9

Pranav
  • 1
  • 1

1 Answers1

0

Use:

df['New'] = pd.to_datetime(df['Hour']).dt.round('1H').dt.hour
print (df)
       Hour  New
0  09:01:00    9
1  09:20:00    9
2  09:35:00   10
3  09:48:00   10
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252