Reputation: 81
I have the following csv file And i had opened at using python pandas as dataframe . I need to modify the file as following : 1 - rename the column (local time ) into Date 2 - delete anything from the column (Date) except the date itself (ex 2.6.2019) 3- change date format into mm/dd/yyyy 4 - export the new file as csv Thanks
Upvotes: 0
Views: 1054
Reputation: 16683
The key parameter for you to pass when using pd.to_datetime is dayfirst=True. Then, use .dt.strftime('%m/%d/%Y') to change to the desired format. I have also given you an example of how to rename a column and read/write to .csv. Again, I understand that you are on mobile, but next time, I would show more effort.
import pandas as pd
# df=pd.read_csv('filename.csv')
# I have manually created a dataframe below, but the above is how you read in a file.
df=pd.DataFrame({'Local time' : ['11.02.2015 00:00:00.000 GMT+0200',
'12.02.2015 00:00:00.000 GMT+0200',
'15.03.2015 00:00:00.000 GMT+0200']})
#Converting string to datetime and changing to desired format
df['Local time'] = pd.to_datetime(df['Local time'],
dayfirst=True).dt.strftime('%m/%d/%Y')
#Example to rename columns
df.rename(columns={'Local time' : 'Date'}, inplace=True)
df.to_csv('filename.csv', index=False)
df
Upvotes: 1