Issue
My csv file contains the date and time values like below. I want to convert this to a datetime format in pandas but I keep getting errors.
I then want to convert it to a 24 hour format with zero padding and without the mention UTC
I already tried : format="%m/%d/%Y %I:%M %p"
10/12/2021 10:25 AM UTC
9/28/2021 8:51 AM UTC
7/27/2021 9:45 AM UTC
2/2/2022 7:10 PM UTC
Desired output:
10/12/2021 10:25
09/28/2021 08:51
07/27/2021 09:45
02/02/2022 19:10
Any help is greatly appreciated
Solution
You can use %m/%d/%Y %H:%M
:
pd.to_datetime(df['date'], dayfirst=False).dt.strftime('%m/%d/%Y %H:%M')
output:
0 10/12/2021 10:25
1 09/28/2021 08:51
2 07/27/2021 09:45
3 02/02/2022 19:10
Name: date, dtype: object
used input:
df = pd.DataFrame({'date': ['10/12/2021 10:25 AM UTC',
'9/28/2021 8:51 AM UTC',
'7/27/2021 9:45 AM UTC',
'2/2/2022 7:10 PM UTC']})
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.