简体   繁体   中英

Convert Pandas column to datetime for a specific datetime format

I would like to convert pandas column that set as a string, to datetime format. How do I convert the column?

  • DataFrame Name: df
  • Column Name: date
  • Value format in column: 2011-06-12T01:17:56
df['datetime'] = pd.to_datetime(df['date'].astype(str) + ' ' + df['time'].astype(str))


df['datetime'] = pd.to_datetime(df['datetime'], format= '%d/%m/%Y %H:%M:%S.%f') 

You can use pd.to_datetime for this.

import pandas as pd
df = pd.DataFrame({"date": ["2011-06-12T01:17:56"]})

Conversion using map method:

df["date"].map(pd.to_datetime)

or

Conversion using apply method:

df["date"].apply(pd.to_datetime)

or

Conversion using function on column series:

df["date"] = pd.to_datetime(df["date"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM