简体   繁体   English

如何在数据框中按月递增日期列?

[英]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.

相关问题 如何在日期列中的熊猫数据框中按月递增日期然后更新数据框 - How to increment date by month in pandas dataframe within a date column then update the dataframe 如何在 PySpark Dataframe 列中将日期转换为月份的第一天? - How to convert date to the first day of month in a PySpark Dataframe column? 如何从DataFrame的日期列中提取月份名称和年份 - How to Extract Month Name and Year from Date column of DataFrame 如何根据日期/月份将 pyspark dataframe 中同一列的行相乘? - How to multiply the rows on a same column in a pyspark dataframe based on date/month? 如何按每个指定的时间自动增加数据框列的时间并按天自动增加日期? - how to auto increment a dataframe column with time by specified time each and auto incrementing date with day? 如何在python中的条件下增加dataframe列? - how to increment a dataframe column under a condition in python? 如何根据列中值的增量对 dataframe 进行切片? - How to slice dataframe based on increment of value in a column? 如何增加 Python Dataframe 中的列名和行名? - How to increment the Column and row names in Python Dataframe? 如何在熊猫的数据框中将月份名称列更改为月份编号列 - how to change the month name column to month number column in dataframe in pandas 日期/时间:对于 dataframe 的每个子集,将日期时间列增加一秒 - date/time: increment the datetime column by one second for each subset of dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM