How can I convert the following string format of datetime into datetime object to be used in pandas Dataframe? I tried many examples, but it seems my format is different from the standard Pandas datetime object. I know this could be a repetition, but I tried solutions on the Stackexchange, but they don't work!
CodePudding user response:
Below code will convert it into appropriate format
df = pd.DataFrame({'datetime':['2013-11-1_00:00','2013-11-1_00:10','2013-11-1_00:20']})
df['datetime_changed'] = pd.to_datetime(df['datetime'].str.replace('_','T'))
df.head()
output:
CodePudding user response:
You can use pd.to_datetime
with format
df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d_%H:%M')