Home > Net >  How to change time format in pandas dataframe
How to change time format in pandas dataframe

Time:11-02

I have txt files with Date and Time two different columns. Current Time format is 02:12:00 (HH:MM:SS)

I want to change above format to 021259 (change SS(00) to 59 as well)

CodePudding user response:

If possible times convert values to strings and then use Series.replace with $ for end of strings - replace seconds and also : to empty string:

print (df)
       Time
0  02:12:00
1  02:12:10

df["Time"] = df["Time"].astype(str).replace({'00$':'59', ':':''}, regex=True)
print (df)
     Time
0  021259
1  021210

CodePudding user response:

After some search I found the solution

df['Time2'] = pd.to_datetime(df['Time'], format='%H:%M:%S').dt.strftime('%H%M' '59')
  • Related