df索引到小时(不能使用to.datetime)

时间:2018-11-08 02:51:04

标签: python pandas datetime dataframe

我想添加新列“时间”,它们是转换形式的索引,例如1到01:00,2到02:00(小时)。我尝试过约会,但它导致了完整的日期,索引仅变成了秒。

df['time'] = df.index

          prob       time
0   9.172968e-01      0
1   8.585100e-01      1
2   7.957514e-01      2
3   7.239329e-01      3
4   6.729420e-01      4
5   6.187557e-01      5
6   5.572768e-01      6

预期结果:

        prob          time
0   9.172968e-01      00:00
1   8.585100e-01      01:00
2   7.957514e-01      02:00
3   7.239329e-01      03:00
4   6.729420e-01      04:00
5   6.187557e-01      05:00
6   5.572768e-01      06:00

2 个答案:

答案 0 :(得分:0)

ValueError: invalid literal for int() with base 10: 'X123' to_timedelta m一起使用

unit

答案 1 :(得分:0)

请检查是否可行。

df['time']=pd.to_timedelta(df.index,unit='m')
df
Out[456]: 
       prob     time
0  0.917297 00:00:00
1  0.858510 00:01:00
2  0.795751 00:02:00
3  0.723933 00:03:00
4  0.672942 00:04:00
5  0.618756 00:05:00
6  0.557277 00:06:00
相关问题