我有一个df,如下所示。
df:
ID open_date limit
1 2020-06-03 100
1 2020-06-23 500
1 2019-06-29 300
1 2018-06-29 400
从上面我想计算一个名为的列age_in_days
。age_in_days
是从open_date到的天数2020-06-30
。
预期产量
ID open_date limit age_in_days
1 2020-06-03 100 27
1 2020-06-23 500 7
1 2019-06-29 300 367
1 2018-06-29 400 732
确保open_date
在datetime dtype中减去2020-06-30
df['open_date'] = pd.to_datetime(df.open_date)
df['age_in_days'] = (pd.Timestamp('2020-06-30') - df.open_date).dt.days
Out[209]:
ID open_date limit age_in_days
0 1 2020-06-03 100 27
1 1 2020-06-23 500 7
2 1 2019-06-29 300 367
3 1 2018-06-29 400 732
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句