I have the following code that is basically changing the format of a UTC timestamp from mm/dd/yyyy hh:mm:ss AM to dd/mm/yyyy hh:mm:ss AM. It is working fine but now I need it to either replace the old column in the same csv file or create another csv file but with the modified date format. The thing is that when I run the code the UTC timestamp column is empty, it has the header though.
Here is the code
import pandas as pd
import datetime
from datetime import datetime
from datetime import time
df = pd.read_csv("C:/Users/javie/OneDrive/Escritorio/22.1.21_P1_English_Language_Y10.csv")
df[' UTC Event Timestamp'] = pd.to_datetime(df[' UTC Event Timestamp'])
df[' UTC Event Timestamp'] = df[' UTC Event Timestamp'].apply(lambda x: print(x.strftime('%d/%m/%Y %I:%M:%S %p')))
df.to_csv("C:/Users/javie/OneDrive/Escritorio/output.csv",index=False,date_format='%d/%m/%Y %I:%M:%S %p')
Your code is almost there.
The usage of print()
is not necessary, that is where your problem comes from.
Remove that and your code should work:
import pandas as pd
import datetime
from datetime import datetime
from datetime import time
df = pd.read_csv("C:/Users/javie/OneDrive/Escritorio/22.1.21_P1_English_Language_Y10.csv")
df[' UTC Event Timestamp'] = pd.to_datetime(df[' UTC Event Timestamp'])
df[' UTC Event Timestamp'] = df[' UTC Event Timestamp'].apply(lambda x: x.strftime('%d/%m/%Y %I:%M:%S %p'))
df.to_csv("C:/Users/javie/OneDrive/Escritorio/output.csv",index=False,date_format='%d/%m/%Y %I:%M:%S %p')