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
Asked
Active
Viewed 752 times
0

Mohamed Abass
- 71
- 1
- 1
- 9
-
1StackOverflow is not a coding service. Although most people who see this post could easily do what you want, you have not made an effort to solve the problem on your own. Please provide sample data in copy/pastable text format (rather than a screenshot), provide your code and explain what you've tried, and where you are specifically struggling with in your code. – David Erickson Mar 08 '20 at 00:57
-
Sorry , i just use phone to write the question – Mohamed Abass Mar 08 '20 at 01:02
-
1As stated by @DavidErickson, please read [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) on how to post a good, reproducible `pandas` example. – Ukrainian-serge Mar 08 '20 at 02:28
1 Answers
1
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

David Erickson
- 16,433
- 2
- 19
- 35