Thursday, May 23, 2024
HomejQueryWrite A number of Knowledge Frames to CSV by Python Pandas

Write A number of Knowledge Frames to CSV by Python Pandas

Python Pandas Knowledge Body to CSV

Within the earlier tutorial, we realized the way to create a CSV file based mostly on Pandas Knowledge body. There, we used a single knowledge body based mostly on a listing after which created a CSV file through the use of Pandas DF’s to_csv technique.

On this tutorial, we’re going to share examples of making CSV information based mostly on a number of knowledge frames.

For the sake of simplicity, we’re utilizing nearly comparable forms of knowledge within the lists.

An instance of making CSV by two knowledge frames

On this instance, we’ve got two product lists that we’ll use to create a CSV file.

See the code and output beneath and we are going to clarify the way it labored.

The code:



How above code work?

  • To begin with, we imported the Pandas library
  • That is adopted by creating two lists with completely different columns
  • Then knowledge frames are created the place we specified these lists and column names
  • Lastly, we used concat technique to concatenate each knowledge frames horizontally.

Writing with out index column

Within the above output pictures, chances are you’ll discover an index column “A” with numbers beginning at 0,1,..

To keep away from this, chances are you’ll use the index = False attribute within the to_csv technique as follows:


Output in Notepad:


An instance of writing knowledge frames one after one other vertically

Chances are you’ll use the append mode in .to_csv technique for writing knowledge vertically. That’s, the second DF knowledge goes down after the primary DF, and so forth.

See the code beneath and output:


Second manner

So as to write second/different knowledge frames vertically, chances are you’ll iterate by means of every knowledge body with Python open technique and append the df.

See an instance beneath the place we are going to write the identical knowledge frames as within the above examples however vertically:






Please enter your comment!
Please enter your name here

Most Popular

Recent Comments