Pandas 带有时间列的数据框

Pandas data frame with time column

我有 df 和时间这样的列:

time
12:30
12:15
12:00

我需要这样的浮点型:

time
12.5
12.25
12.00

我试过:

df.time = df.time.replace(":", ".", regex=True).astype(float)

但我的结果并不令人满意,像这样:

time
12.3
12.15
12.00

有什么想法吗?

一种解决方案是使用 pandas.to_timedelta on a modified version of the input string (to match the expected format), then convert to total_seconds 并除以 60:

df['time_float'] = pd.to_timedelta('00:'+df['time']).dt.total_seconds()/60

输出:

    time  time_float
0  12:30       12.50
1  12:15       12.25
2  12:00       12.00

按照您的原意使用 str.replace 的替代方法:

df['time'] = (df['time']
              .str.replace('(:)(\d+)',
                           lambda x: f'.{100*int(x.group(2))//60}',
                           regex=True)
              .astype(float)
              )

使用 to_timedelta with append :00 by Series.add, then convert to seconds by Series.dt.total_seconds 最后除以 60:

df['time'] = pd.to_timedelta(df['time'].add(':00')).dt.total_seconds().div(3600)
print (df)
0  12.50
1  12.25
2  12.00