Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

The default output format of to_csv() is:

12/14/2012  12:00:00 AM

I cannot figure out how to output only the date part with the specific format:

20121214

or date and time in two separate columns in the csv file:

20121214,  084530

The documentation is too brief to give me any clue as to how to do these. Can anyone help?

1 Answer

0 votes
by (108k points)

You have to use strftime to save them in separate columns:

df['date'] = df['datetime'].apply(lambda x: x.strftime('%d%m%Y'))

df['time'] = df['datetime'].apply(lambda x: x.strftime('%H%M%S'))

and to export particular column to csv, refer the following code:

df[['date', 'time', ... ]].to_csv('df.csv')

For more information regarding the same refer to the following link: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 13, 2019 in Web Technology by Sammy (47.6k points)
+1 vote
1 answer

Browse Categories

...