将hh:mm转换为float pandas

2024-04-25 16:59:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧(第一列是string格式):`

Time_string     Count 
23:10           2400
02:55           760

我想添加第三列,即第一列的float

^{pr2}$

我该怎么做?在


Tags: 数据stringtime格式countfloatpr2
1条回答
网友
1楼 · 发布于 2024-04-25 16:59:13

这很简单,只要你的时间戳只有小时和分钟的组成部分。分开,操作,然后合并-

y = df.Time_string.str.split(':')   
i, j = y.str[0], y.str[1]  

df['Time_float'] = i.astype(int) + (j.astype(float) / 60) 

df
  Time_string  Count  Time_float
0       23:10   2400   23.166667
1       02:55    760    2.916667

相关问题 更多 >