2 回答
TA贡献1853条经验 获得超9个赞
应该这样做:
df['date1'] = pd.to_datetime(df['date1'])
df['date2'] = pd.to_datetime(df['date2'])
df['difference'] = (df['date1']-df['date2']).dt.days
print(df)
date1 date2 difference
0 2020-02-28 2020-03-31 -32
TA贡献1719条经验 获得超6个赞
请参见下面的示例:
df.head()
date1 date2
0 2020-01-07 08:24:25 2020-07-28 01:34:44
1 2020-01-06 10:32:18 2020-03-21 17:13:07
2 2020-01-07 08:34:01 2020-03-21 17:13:09
3 2020-05-02 11:13:18 2020-07-18 21:57:11
4 2020-01-11 12:56:22 2020-04-02 21:28:15
#creating diff column:
df['diff']=(df["date1"]-df["date2"]).dt.days
df.head()
#it results on this:
date1 date2 diff
0 2020-01-07 08:24:25 2020-07-28 01:34:44 202
1 2020-01-06 10:32:18 2020-03-21 17:13:07 75
2 2020-01-07 08:34:01 2020-03-21 17:13:09 74
3 2020-05-02 11:13:18 2020-07-18 21:57:11 77
4 2020-01-11 12:56:22 2020-04-02 21:28:15 82
添加回答
举报