如何将日期时间格式转换为分钟- pandas [英] How to convert a datetime format to minutes - pandas

查看:761
本文介绍了如何将日期时间格式转换为分钟- pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,该数据框具有一列usage_duration(这是日期时间格式的另外两列的差).如下图所示:

I have a data frame which has a column usage_duration (which is the difference of two another columns in datetime format). It looks like below:

processid, userid, usage_duration 
17613,root,0 days 23:41:03.000000000
17641,root,2 days 04:05:26.000000000
13848,acs,0 days 00:00:50.000000000
3912,acs,0 days 06:07:38.000000000
6156,acs,0 days 17:22:43.000000000

现在我想将其转换为分钟.它应该如下所示:

Now I wanted to convert the same into minutes. It should look like as below:

processid, userid, usage_duration_min
17613,root,1421
17641,root,3125
13848,acs,0
3912,acs,367
6156,acs,1042

有人能让我知道这怎么可能吗?

Can someone let me know how is it possible?

非常感谢您的支持

推荐答案

使用 total_seconds seconds 除以60,最后转换为integer s:

Use total_seconds or seconds and divide by 60, last cast to integers:

#if necessary converting to timedelta
#df['usage_duration'] = pd.to_timedelta(df['usage_duration'])

df['new'] = df['usage_duration'].dt.total_seconds().div(60).astype(int)

或者:

 df['new'] = (df['usage_duration'].dt.seconds.div(60).astype(int) 
             + df['usage_duration'].dt.days.multiply(1440).astype(int) )

print (df)
   processid userid  usage_duration   new
0      17613   root 0 days 23:41:03  1421
1      17641   root 2 days 04:05:26  3125
2      13848    acs 0 days 00:00:50     0
3       3912    acs 0 days 06:07:38   367
4       6156    acs 0 days 17:22:43  1042

这篇关于如何将日期时间格式转换为分钟- pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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