I have a Pandas dataframe where, column name ‘A’ has date and time value (as of now it is of type string).
Column A | Column B |
---|---|
2024-07-11 13:09:37.466 | PC2 |
2024-07-11 13:24:43.03 | PC1 |
May 6 2024 22:49:50 | PC5 |
Desired Ouput
-
I am trying to remove milliseconds from time and also only one format which is %Y-%m-%d %H:%M:%S. So the output should be like below:
| Column A | Column B |
| ——————- | ——– |
| 2024-07-11 13:09:37 | PC2 |
| 2024-07-11 13:24:43 | PC1 |
| 2024-05-06 22:49:50 | PC5 | -
Convert Column A into type datetime, so I can sort later by datetime
Tried Solution
I tried below below solutions but both are giving error. I think due to number of different digits in milliseconds
df['Column A'] = pd.to_datetime(Df['event time']).dt.strftime('%Y-%m-%d %H:%M:%S')
df['Column A'] = Df['Column A'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
Kindly if you can help