O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving DataFrame to a CSV file

CSV files can be saved from DataFrame using the .to_csv() method. To demonstrate saving data to a CSV file, we will save the df2 object with the revised column names to a new file named data/msft_modified.csv:

It is necessary to tell the method that the index label should be saved with the column name of date using index_label='date'. Otherwise, the index does not have a name added to the first row of the file, which will make it difficult to read back properly.

To examine that this worked properly, we can explore the new file to view some of its content using the !head command (and if on a Windows system, use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required