简体   繁体   English

如果月份是一月,如何仅更改 dataframe 列(日期时间)中的年份?

[英]How to change only the year in a dataframe column (datetime) if the month is january?

I have a dataframe with some dates in a column.我有一个 dataframe,列中有一些日期。 I would like to set the year to 2021 if the month is January, as I have some errors in the Data I am processing with people putting January 2020.如果月份是 1 月,我想将年份设置为 2021 年,因为我正在处理的数据中有一些错误,而人们将 2020 年 1 月设置为 2020 年 1 月。

    Port Of Loading  ETA Destination Port
2         Qingdao    2020-01-09 00:00:00
3         Qingdao    2020-01-16 00:00:00
4         Shenzhen   2020-12-31 00:00:00

Would become:会成为:

    Port Of Loading  ETA Destination Port
2         Qingdao    2021-01-09 00:00:00
3         Qingdao    2021-01-16 00:00:00
4         Shenzhen   2020-12-31 00:00:00

I tried with:我试过:

    if df[df['ETA Destination Port']].month == 1:
        januarys = df[df['ETA Destination Port']].month == 1
        januarys.year = 2021
        df = np.where(df['ETA Destination Port'].month == 1, df['ETA Destination Port'], januarys)

But I get the error:但我得到了错误:

KeyError: "None of [DatetimeIndex(['2020-11-19', '2020-12-03', '2020-12-10'], dtype='datetime64[ns]', freq=None)] are in the [columns]" KeyError:“[DatetimeIndex(['2020-11-19', '2020-12-03', '2020-12-10'], dtype='datetime64[ns]', freq=None)] 中没有一个[列]"

Any help appreciated:)任何帮助表示赞赏:)

You can try the below code:你可以试试下面的代码:

import pandas as pd

csvfile = pd.read_csv("input.csv")

# Extract dates in separated columns
csvfile['Day'] = pd.to_datetime(csvfile['ETA Destination Port']).dt.day
csvfile['Month'] = pd.to_datetime(csvfile['ETA Destination Port']).dt.month
csvfile['Year'] = pd.to_datetime(csvfile['ETA Destination Port']).dt.year

# Change year to 2021 when month is January
csvfile.loc[csvfile['Month'] == 1, 'Year'] = 2021

# Concatenate values into a single column and drop irrelevant
### If you want to display time as well as date
# csvfile['ETA Destination Port'] = pd.to_datetime(csvfile[['Year', 'Month', 'Day']]).dt.strftime('%Y-%m-%d %H:%M:%S')
### If you want to keep it as a datetime format
csvfile['ETA Destination Port'] = pd.to_datetime(csvfile[['Year', 'Month', 'Day']])
csvfile = csvfile.drop(columns=['Day', 'Month', 'Year'])

Probably not the most efficient way to proceed as I am not a pandas master but should do the trick.可能不是最有效的方法,因为我不是 pandas 大师,但应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM