Hit enter after type your search item

Write Multiple Data Frames to CSV by Python Pandas to_csv

Python Pandas Data Frame to CSV

In the previous tutorial, we learned how to create a CSV file based on Pandas Data frame. There, we used a single data frame based on a list and then created a CSV file by using Pandas DF’s to_csv method.

In this tutorial, we are going to share examples of creating CSV files based on multiple data frames.

For the sake of simplicity, we are using almost similar types of data in the lists.

An example of creating CSV by two data frames

In this example, we have two product lists that we will use to create a CSV file.

See the code and output below and we will explain how it worked.

The code:


Output:

Python-csv-multiple-df

How above code work?

  • First of all, we imported the Pandas library
  • This is followed by creating two lists with different columns
  • Then data frames are created where we specified those lists and column names
  • Finally, we used concat method to concatenate both data frames horizontally.

Writing without index column

In the above output images, you may notice an index column “A” with numbers starting at 0,1,..

To avoid this, you may use the index = False attribute in the to_csv method as follows:

Code:


Output in Notepad:

Python-csv-index-false

An example of writing data frames one after another vertically

You may use the append mode in .to_csv method for writing data vertically. That is, the second DF data goes down after the first DF, and so on.

See the code below and output:


Output:

Python-csv-multi-appen

Second way

In order to write second/other data frames vertically, you may iterate through each data frame with Python open method and append the df.

See an example below where we will write the same data frames as in the above examples but vertically:


 

Result:

Python-csv-vertical-mu

This div height required for enabling the sticky sidebar