How to convert variable length datetime column to datetime dtype in pandas dataframe

  Kiến thức lập trình

I have a column of data that is formatted as XXh:XXm:XXs but the hours/minutes/seconds can be different lengths depending on their values

Example:
123h:23m:1s
1h:1m:1s
12h:0m:14s

How can I convert a column to datetime with these variable lengths.

I have tried:

df['col'] = pd.to_datetime(df['col'], format='HHh:MMm:SSs')

But it fails on the first row that doesn’t match that specific length for HH, MM, SS exactly.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT