将HH:MM pandas 中的列转换为分钟 [英] Convert a column in pandas of HH:MM to minutes

查看:62
本文介绍了将HH:MM pandas 中的列转换为分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将hh:mm格式的数据集中的列转换为分钟.我尝试了以下代码,但它显示"AttributeError:'Series'对象没有属性'split'".数据采用以下格式.我在数据集中也有nan值,计划是计算值的中位数,然后用中位数填充具有nan的行

I want to convert a column in dataset of hh:mm format to minutes. I tried the following code but it says " AttributeError: 'Series' object has no attribute 'split' ". The data is in following format. I also have nan values in the dataset and the plan is to compute the median of values and then fill the rows which has nan with the median

02:32
02:14
02:31
02:15
02:28
02:15
02:22
02:16
02:22
02:14

到目前为止,我已经尝试过

I have tried this so far

 s = dataset['Enroute_time_(hh mm)']

   hours, minutes = s.split(':')
   int(hours) * 60 + int(minutes)

推荐答案

我建议您避免按行计算.您可以对Pandas/NumPy使用矢量化方法:

I suggest you avoid row-wise calculations. You can use a vectorised approach with Pandas / NumPy:

df = pd.DataFrame({'time': ['02:32', '02:14', '02:31', '02:15', '02:28', '02:15', 
                            '02:22', '02:16', '02:22', '02:14', np.nan]})

values = df['time'].fillna('00:00').str.split(':', expand=True).astype(int)
factors = np.array([60, 1])

df['mins'] = (values * factors).sum(1)

print(df)

     time  mins
0   02:32   152
1   02:14   134
2   02:31   151
3   02:15   135
4   02:28   148
5   02:15   135
6   02:22   142
7   02:16   136
8   02:22   142
9   02:14   134
10    NaN     0

这篇关于将HH:MM pandas 中的列转换为分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆