You can use the strptime
and strftime
methods from the built-in datetime
module to use in the lambda
function that will go into the brackets of the apply
method:
from datetime import datetime
import pandas as pd
df = {"column_1": ["XYZ", "QAZ", "WSX"],
"date": ["04/11/2016 09:13:55 AM", "05/10/2019 11:31:05 PM", "12/08/2019 08:00:00 AM"]}
df = pd.DataFrame(df)
format1 ="%m/%d/%Y %I:%M:%S %p"
format2 = "%Y-%m-%d %H:%M:%S"
df["date"] = df["date"].apply(lambda x: datetime.strptime(x, format1).strftime(format2))
print(df)
Output:
column_1 date
0 XYZ 2016-04-11 09:13:55
1 QAZ 2019-05-10 23:31:05
2 WSX 2019-12-08 08:00:00
UPDATE:
As pointed out by @YevKad in the comments, there is no need to import datetime
, as pandas
comes with its own datetime
section:
import pandas as pd
df = {"column_1": ["XYZ", "QAZ", "WSX"],
"date": ["04/11/2016 09:13:55 AM", "05/10/2019 11:31:05 PM", "12/08/2019 08:00:00 AM"]}
df = pd.DataFrame(df)
format1 ="%m/%d/%Y %I:%M:%S %p"
format2 = "%Y-%m-%d %H:%M:%S"
df['date'] = pd.to_datetime(df['date'], format=format1).dt.strftime(format2)
print(df)