Combine month name and year in a column pandas python

huangapple go评论84阅读模式
英文:

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_periodSeries.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')

huangapple
  • 本文由 发表于 2020年1月6日 20:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定