I am performing some data cleaning to my dataframe and there are two columns labelled Tranc_Year and Tranc_Month.
The two columns are originally of int64 datatype and I want to convert them to datetime.
However, after converting it to datetime with the following code:
df_train.loc[:,'Tranc_Year'] = pd.to_datetime(df_train['Tranc_Year'], format='%Y') df_train.loc[:,'Tranc_Month'] = pd.to_datetime(df_train['Tranc_Month'], format='%m')
The display becomes as follows:
How do i convert to date time while maintaining the original display?
CodePudding user response:
Use to_datetime
with columns year, month, day
by rename
by dictionary:
d = {'Tranc_Year':'year','Tranc_Month':'month'}
df1 = df_train[['Tranc_Year', 'Tranc_Month']].rename(columns=f).assign(day=1)
df_train['date'] = pd.to_datetime(df1)