英文:
Combine month name and year in a column pandas python
问题
Year Month Name Avg Month Name-Year
2015 Jan 12 2015-Jan
2015 Feb 13.4 2015-Feb
2015 Mar 10 2015-Mar
...................
2019 Nov 11 2019-Nov
2019 Dec 11 2019-Dec
英文:
df
Year Month Name Avg
2015 Jan 12
2015 Feb 13.4
2015 Mar 10
...................
2019 Jan 11
2019 Feb 11
Code
df['Month Name-Year']= pd.to_datetime(df['Month Name'].astype(str)+df['Year'].astype(str),format='%b%Y')
In the dataframe, df, the groupby output avg is on keys month name and year. So month name and year are actually multilevel indices. I want to create a third column Month Name Year so that I can do some operation (create plots etc) using the data.
The output I am getting using the code is as below:
Year Month Name Avg Month Name-Year
2015 Jan 12 2015-01-01
2015 Feb 13.4 2015-02-01
2015 Mar 10 2015-03-01
...................
2019 Nov 11 2019-11-01
2019 Dec 11 2019-12-01
and so on.
The output I want is 2015-Jan, 2015-Feb etc in Month Name-Year column...or I want 2015-01, 2015-02...2019-11, 2019-12 etc (only year and month, no days).
Please help
答案1
得分: 6
一种解决方法是将日期转换为日期时间,然后使用Series.dt.to_period
或Series.dt.strftime
更改格式:
df['Month Name-Year']=pd.to_datetime(df['Month Name']+df['Year'].astype(str),format='%b%Y')
# 对于月份周期
df['Month Name-Year1'] = df['Month Name-Year'].dt.to_period('m')
# 对于2010-02格式
df['Month Name-Year2'] = df['Month Name-Year'].dt.strftime('%Y-%m')
最简单的解决方法是不进行日期时间转换,只是使用连字符-
连接,并将年份转换为字符串:
# 格式为2010-Feb
df['Month Name-Year3'] = df['Year'].astype(str) + '-' + df['Month Name']
这与先转换为日期时间,然后转换为自定义字符串的方式相同:
# 格式为2010-Feb
df['Month Name-Year31'] = df['Month Name-Year'].dt.strftime('%Y-%b')
英文:
One type of solution is converting to datetimes and then change format by Series.dt.to_period
or Series.dt.strftime
:
df['Month Name-Year']=pd.to_datetime(df['Month Name']+df['Year'].astype(str),format='%b%Y')
#for months periods
df['Month Name-Year1'] = df['Month Name-Year'].dt.to_period('m')
#for 2010-02 format
df['Month Name-Year2'] = df['Month Name-Year'].dt.strftime('%Y-%m')
Simpliest is solution without convert to datetimes only join with -
and convert years to strings:
#format 2010-Feb
df['Month Name-Year3'] = df['Year'].astype(str) + '-' + df['Month Name']
...what is same like converting to datetimes and then converting to custom strings:
#format 2010-Feb
df['Month Name-Year31'] = df['Month Name-Year'].dt.strftime('%Y-%b')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论