0

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 enter image description here

3
  • 1
    StackOverflow 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. Commented Mar 8, 2020 at 0:57
  • Sorry , i just use phone to write the question Commented Mar 8, 2020 at 1:02
  • 1
    As stated by @DavidErickson, please read this on how to post a good, reproducible pandas example. Commented Mar 8, 2020 at 2:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.