简体   繁体   中英

Combine Month name and Year in one column

I need to combine Month name and Year column in one column 'Month-Year' in datetime format (Day,Month,Year). Been having issues with NaN and data types. Also need to assign default day as 1 for all rows.

Current data types:

Month: (O)
Year: float64
df1=pd.DataFrame({'Month':["January"," ","December","February"," "," ","March","July"],
'Year':["2020"," ","2017","2015"," "," ","2019","2015"]})

Final Dataframe

在此处输入图像描述

Try:

df1 = df1.replace(r'\s+', np.nan, regex=True)
df1['Month-Year'] = pd.to_datetime(df1['Month']+ '-'+ df1['Year']).dt.strftime('%d/%m/%Y')

df1:

    Month       Year    Month-Year
0   January     2020    01/01/2020
1   NaN         NaN     NaN
2   December    2017    01/12/2017
3   February    2015    01/02/2015
4   NaN         NaN     NaN
5   NaN         NaN     NaN
6   March       2019    01/03/2019
7   July        2015    01/07/2015

One thing u could do is Create a column(Month-Integer) such that January to 01, February to 02, and so-on. Then use this column and Year column to create Month-Year Column, then if you want you can delete the (Month-Integer) Column.

Try this:

df1["date"] = pd.to_datetime(df1.Month, format='%B', errors='coerce').dt.date
df1.apply(lambda x: x.date.replace(year=int(float(x.Year))) if x.Year!= " " else "",axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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