我有一个时间值为0,..,23的熊猫数据。如何使用字符串操作将时间值转换为范围?
例如。
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至
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发布于 2022-03-02 12:08:28
只需使用:
df['hours_str'] = df['hours'].astype(str)+'-'+df['hours'].add(1).mod(24).astype(str)产出:
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-15https://stackoverflow.com/questions/71322213
复制相似问题