我有一個時間值為 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
uj5u.com熱心網友回復:
只需使用:
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-15
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436869.html
下一篇:存盤日期時間修改 格式
