且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在DataFrame中查找两列之间的时差

更新时间:2022-12-09 13:36:01

使用 to_datetime ,然后您可以减去列以在 dt.days 以获取总天数,例如:

Convert the columns using to_datetime then you can subtract the columns to produce a timedelta on the abs values, then you can call dt.days to get the total number of days, example:

In [119]:
import io
import pandas as pd
t="""Test Date,Test Type,First Use Date
2011-02-05,A,2010-01-05
2012-02-05,A,2010-03-05
2013-02-05,A,2010-06-05
2014-02-05,A,2010-08-05"""
df = pd.read_csv(io.StringIO(t))
df
Out[119]:
    Test Date Test Type First Use Date
0  2011-02-05         A     2010-01-05
1  2012-02-05         A     2010-03-05
2  2013-02-05         A     2010-06-05
3  2014-02-05         A     2010-08-05

In [121]:    
df['Test Date'] = pd.to_datetime(df['Test Date'])
df['First Use Date'] = pd.to_datetime(df['First Use Date'])
df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 4 entries, 0 to 3
Data columns (total 3 columns):
Test Date         4 non-null datetime64[ns]
Test Type         4 non-null object
First Use Date    4 non-null datetime64[ns]
dtypes: datetime64[ns](2), object(1)
memory usage: 128.0+ bytes

In [122]:
df['days'] = (df['Test Date'] - df['First Use Date']).abs().dt.days
df

Out[122]:
   Test Date Test Type First Use Date  days
0 2011-02-05         A     2010-01-05   396
1 2012-02-05         A     2010-03-05   702
2 2013-02-05         A     2010-06-05   976
3 2014-02-05         A     2010-08-05  1280