I have my CSV data as below and would like to read the csv with correct datetime format using python pandas into a dataframe
input
1/1/2022 0:00,5,D1,0
2/15/2022 0:00,10,C1,0
output
1/1/2022 00:00:00,5,D1,0
2/15/2022 00:00:00,10,C1,0
I have my CSV data as below and would like to read the csv with correct datetime format using python pandas into a dataframe
input
1/1/2022 0:00,5,D1,0
2/15/2022 0:00,10,C1,0
output
1/1/2022 00:00:00,5,D1,0
2/15/2022 00:00:00,10,C1,0
What you want exactly is unclear, if you just want to add zeros handling the data as string:
(pd.read_csv('input.csv', header=None)
.rename(columns=str) # trick to be able to use assign with range index
.assign(**{'0': lambda d: d['0']+':00'})
.to_csv('output.csv', index=False, header=None)
)
If you want to handle the dates as datetime:
(pd.read_csv('input.csv', header=None, parse_dates=[0])
.rename(columns=str)
.assign(**{'0': lambda d: d['0'].dt.strftime('%-m/%-d/%Y %H:%M:%S')})
.to_csv('output.csv', index=False, header=None)
)
output:
1/1/2022 0:00:00,5,D1,0
2/15/2022 0:00:00,10,C1,0