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’)