[英]How to increment date column by month in a dataframe?
I have a dataset that has a column named "date_col".我有一个数据集,其中有一列名为“date_col”。 date_col has 1000+ dates, and the dates are only: 01/01/2011 and 01/01/2012.
date_col 有 1000+ 个日期,日期只有:01/01/2011 和 01/01/2012。
This is what I tried:这是我尝试过的:
table['date_col'] = pd.date_range(start='3/1/2011',
periods=len(table['date_col']), freq='d')
How can I easily change the months to be something else than just 01/01?我如何轻松地将月份更改为 01/01 以外的其他月份?
You can parse the date column into a datetime
object so you can manipulate it easily, something like this should work:您可以解析日期列到
datetime
对象,因此您可以轻松操控它,这样的事情应该工作:
from datetime import datetime
date_str = '01/01/2012'
date_format = '%d/%m/%Y'
date = datetime.strptime(date_str, date_format)
# just change the day
date = date.replace(day=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.