0

I have a df like this:

Sr.  lwd_month lwd_year
1     3        2015
2     6        2018
3.    9        2017
4.    NaN      NaN
5.    5        2015

How can I merge this two columns to get dataframe like below?:

Sr.  lwd_month   lwd_Year   MonthYear
1     3          2015    03-2015
2     6          2018     06-2018
3.    9          2017     09-2017
4.    NaN        NaN      NaT
5.    5          2015     05-2015
6.    3          NaN      NaT

Thanks

Sociopath
  • 13,068
  • 19
  • 47
  • 75

2 Answers2

2

First need columns names with lowercase year and month and pandas version 0.18.1+.

Then use to_datetime for convert by multiple columns with strftime for strings:

df['MonthYear']=pd.to_datetime(df.assign(day=1)[['year','month','day']]).dt.strftime('%m-%Y')
print (df)
   Sr.  month    year MonthYear
0  1.0    3.0  2015.0   03-2015
1  2.0    6.0  2018.0   06-2018
2  3.0    9.0  2017.0   09-2017
3  4.0    NaN     NaN       NaT
4  5.0    5.0  2015.0   05-2015

print (type(df.loc[0, 'MonthYear']))
<class 'str'>

Similar for month period use to_period:

df['MonthYear'] = pd.to_datetime(df.assign(day=1)[['year','month','day']]).dt.to_period('m')
print (df)
   Sr.  month    year MonthYear
0  1.0    3.0  2015.0   2015-03
1  2.0    6.0  2018.0   2018-06
2  3.0    9.0  2017.0   2017-09
3  4.0    NaN     NaN       NaT
4  5.0    5.0  2015.0   2015-05

print (type(df.loc[0, 'MonthYear']))
<class 'pandas._libs.tslibs.period.Period'>
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

Why not just this:

df['MonthYear'] = pd.to_datetime(df[['Year', 'Month']].assign(Day=1)).dt.strftime('%m-%Y')
print(df)

Output:

   Sr.  Month    Year MonthYear
0  1.0    3.0  2015.0   03-2015
1  2.0    6.0  2018.0   06-2018
2  3.0    9.0  2017.0   09-2017
3  4.0    NaN     NaN       NaT
4  5.0    5.0  2015.0   05-2015
U13-Forward
  • 69,221
  • 14
  • 89
  • 114