pandas DataFrame中两个日期之间的差异 [英] Difference between two dates in Pandas DataFrame
本文介绍了 pandas DataFrame中两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据框中有很多列,我必须在名为 in_time
和 out_time
的两列中找到时间差,并将其放入新的列中相同的数据帧.
I have many columns in a data frame and I have to find the difference of time in two column named as in_time
and out_time
and put it in the new column in the same data frame.
时间格式类似于 2015-09-25T01:45:34.372Z
.
我正在使用Pandas DataFrame.
I am using Pandas DataFrame.
我想这样做:
df.days = df.out_time - df.in_time
我有很多列,我必须在其中增加1个名为days的列,并将差异放在其中.
I have many columns and I have to increase 1 more column in it named days and put the differences there.
推荐答案
您需要将字符串转换为 datetime
dtype,然后可以减去所需的任意日期,并在结果系列调用中 dt.days
:
You need to convert the strings to datetime
dtype, you can then subtract whatever arbitrary date you want and on the resulting series call dt.days
:
In [15]:
df = pd.DataFrame({'date':['2015-09-25T01:45:34.372Z']})
df
Out[15]:
date
0 2015-09-25T01:45:34.372Z
In [19]:
df['date'] = pd.to_datetime(df['date'])
df['day'] = (df['date'] - dt.datetime.now()).dt.days
df
Out[19]:
date day
0 2015-09-25 01:45:34.372 -252
这篇关于 pandas DataFrame中两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文