简体   繁体   English

Pandas 1.0 从年份和日期创建月份列

[英]Pandas 1.0 create column of months from year and date

I have a dataframe df with values as:我有一个数据框df ,其值为:

df.iloc[1:4, 7:9]
    Year  Month
38  2020      4
65  2021      4
92  2022      4

I am trying to create a new MonthIdx column as:我正在尝试创建一个新的MonthIdx列:

df['MonthIdx'] = pd.to_timedelta(df['Year'], unit='Y') + pd.to_timedelta(df['Month'], unit='M') + pd.to_timedelta(1, unit='D')

But I get the error:但我收到错误:

ValueError: Units 'M' and 'Y' are no longer supported, as they do not represent unambiguous timedelta values durations.

Following is the desired output:以下是所需的输出:

df['MonthIdx']
    MonthIdx
38  2020/04/01
65  2021/04/01
92  2022/04/01

So you can pad the month value in a series, and then reformat to get a datetime for all of the values:因此,您可以在一系列中填充月份值,然后重新格式化以获取所有值的日期时间:

month = df.Month.astype(str).str.pad(width=2, side='left', fillchar='0')
df['MonthIdx'] = pd.to_datetime(pd.Series([int('%d%s' % (x,y)) for x,y in zip(df['Year'],month)]),format='%Y%m')

This will give you:这会给你:

   Year  Month   MonthIdx
0  2020      4 2020-04-01
1  2021      4 2021-04-01
2  2022      4 2022-04-01

You can reformat the date to be a string to match exactly your format:您可以将日期重新格式化为字符串以完全匹配您的格式:

df['MonthIdx'] = df['MonthIdx'].apply(lambda x: x.strftime('%Y/%m/%d'))

Giving you:给你:

   Year  Month    MonthIdx
0  2020      4  2020/04/01
1  2021      4  2021/04/01
2  2022      4  2022/04/01

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

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