从大 pandas 的datetime列减去一年 [英] Subtract an year from a datetime column in pandas
本文介绍了从大 pandas 的datetime列减去一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个datetime列如下 -
I have a datetime column as below -
>>> df['ACC_DATE'].head(2)
538 2006-04-07
550 2006-04-12
Name: ACC_DATE, dtype: datetime64[ns]
现在,我想从这一列的每一行减去一年。如何达到同样的目的?我可以使用哪个图书馆?
Now, I want to subtract an year from each row of this column. How can I achieve the same & which library can I use?
预期字段 -
ACC_DATE NEW_DATE
538 2006-04-07 2005-04-07
549 2006-04-12 2005-04-12
谢谢。
推荐答案
你可以使用 DateOffset
实现这一点:
You can use DateOffset
to achieve this:
In [15]:
df['NEW_DATE'] = df['ACC_DATE'].apply(lambda x: x - pd.DateOffset(years=1))
df
Out[15]:
ACC_DATE NEW_DATE
index
538 2006-04-07 2005-04-07
550 2006-04-12 2005-04-12
这篇关于从大 pandas 的datetime列减去一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文