I have a pandas dataframe with time values 0,..,23. How can I convert the the time values to a range using string manipulation?
ex.
print(hours)
0 17
1 0
2 11
3 19
4 13
..
1458639 13
1458640 7
1458641 6
1458642 15
1458643 14
Name: datetime_hour, Length: 1458644, dtype: int64
to
print(hours_str)
0 17-18
1 0-1
2 11-12
3 19-20
4 13-14
..
1458639 13-14
1458640 7-8
1458641 6-7
1458642 15-16
1458643 14-15
Name: datetime_hour, Length: 1458644, dtype: str
CodePudding user response:
Simply use:
df['hours_str'] = df['hours'].astype(str) '-' df['hours'].add(1).mod(24).astype(str)
output:
hours hours_str
0 17 17-18
1 0 0-1
2 11 11-12
3 19 19-20
4 13 13-14
1458639 13 13-14
1458640 7 7-8
1458641 6 6-7
1458642 15 15-16
1458643 14 14-15