Generating a new date column from the month, day and year columns in data table using Python Pandas.

When working on datasets, sometimes we have situation where year, month and day part of a date variable are stored separately as integers in three different columns instead of a single properly formatted column with date values. To work on the dates in that case, we’ll need to create a new date column from the three columns. To do so we first need to copy three columns into a new data frame (df2) using copy function nd then use to_datetime function to generate new column containing dates.

Copy year, month and day columns to new data frame.

df2 = df[[‘view_year’,’view_month’,’view_day’]].copy()

Change column names to “year”, “month” and “day”.

df2.columns = [“year”, “month”, “day”]

Use to_date time function to create required date column.

df[‘new_date’]=pd.to_datetime(df2 , errors=’coerce’)

2 thoughts on “Generating a new date column from the month, day and year columns in data table using Python Pandas.

Leave a Reply

Your email address will not be published. Required fields are marked *