0

I have a dataframe as shown below

Name       Birth_Year
Messi      2007
Ronaldo    2004  

I would like to convert that Birth_Year into date.

Expected Output

Name    Birt_Year      Birth_Date
Messi   2007.0         2007-01-01
Ronaldo 2004.0         2004-01-01

I tried below code

df1['Year'] = pd.to_datetime(df1['Birt_Year'], format='%Y%m%d.0', errors='coerce')

1 Answer 1

3

You were almost there, your format is just %Y, since you only have year on your Birth_Year column:

df['Birth_Date'] = pd.to_datetime(df['Birth_Year'], format='%Y')

      Name  Birth_Year Birth_Date
0    Messi        2007 2007-01-01
1  Ronaldo        2004 2004-01-01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.