Combine month name and year in a column pandas python

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

Combine month name and year in a column pandas python

问题

  1. Year Month Name Avg Month Name-Year
  2. 2015 Jan 12 2015-Jan
  3. 2015 Feb 13.4 2015-Feb
  4. 2015 Mar 10 2015-Mar
  5. ...................
  6. 2019 Nov 11 2019-Nov
  7. 2019 Dec 11 2019-Dec
英文:

df

  1. Year Month Name Avg
  2. 2015 Jan 12
  3. 2015 Feb 13.4
  4. 2015 Mar 10
  5. ...................
  6. 2019 Jan 11
  7. 2019 Feb 11

Code

  1. 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:

  1. Year Month Name Avg Month Name-Year
  2. 2015 Jan 12 2015-01-01
  3. 2015 Feb 13.4 2015-02-01
  4. 2015 Mar 10 2015-03-01
  5. ...................
  6. 2019 Nov 11 2019-11-01
  7. 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更改格式:

  1. df['Month Name-Year']=pd.to_datetime(df['Month Name']+df['Year'].astype(str),format='%b%Y')
  2. # 对于月份周期
  3. df['Month Name-Year1'] = df['Month Name-Year'].dt.to_period('m')
  4. # 对于2010-02格式
  5. df['Month Name-Year2'] = df['Month Name-Year'].dt.strftime('%Y-%m')

最简单的解决方法是不进行日期时间转换,只是使用连字符-连接,并将年份转换为字符串:

  1. # 格式为2010-Feb
  2. df['Month Name-Year3'] = df['Year'].astype(str) + '-' + df['Month Name']

这与先转换为日期时间,然后转换为自定义字符串的方式相同:

  1. # 格式为2010-Feb
  2. 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:

  1. df['Month Name-Year']=pd.to_datetime(df['Month Name']+df['Year'].astype(str),format='%b%Y')
  2. #for months periods
  3. df['Month Name-Year1'] = df['Month Name-Year'].dt.to_period('m')
  4. #for 2010-02 format
  5. 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:

  1. #format 2010-Feb
  2. df['Month Name-Year3'] = df['Year'].astype(str) + '-' + df['Month Name']

...what is same like converting to datetimes and then converting to custom strings:

  1. #format 2010-Feb
  2. 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:

确定