Pandas DataFrame to CSV

Previous
Next

In this post, we will see how to save DataFrame to a CSV file in Python pandas.


You can use DataFrame.to_csv() to save DataFrame to CSV file. We will see various options to write DataFrame to csv file.

Syntax of DataFrame.to_csv()

Here,
path_or_buf: Path where you want to write CSV file including file name. If you do not pass this parameter, then it will return String.
sep: Field delimter from output file. Default value is ,
na_rep: Missing data representation.
float_format: To format float point numbers, you can use this parameter.
columns: Columns to write to CSV file.
header: Write out column names.
index: To avoid index(row no) column, you can passindex=False
index_labelColumn label for index column.
We will see meaning of few important attributes with the help of example.

DataFrame sample data

Here is sample data which are going to write to CSV file.

Name Age Gender
Mary 36 Female
John 29 Male
Martin 31 Male

Convert pandas DataFrame to csv

Here are steps to write DataFrame to CSV file.

  1. Import pandas module using import statement
  2. Create a DataFrame as per above sample data.
  3. Decide the path where you want to save file and pass it to to_csv function.
    You can specify either a relative path or an absolute path.
    Relative path:

    Absolute path:

Here is the complete python program to save CSV file.

When you run the above program, Employees.csv will be created a specified path.
Here is data for the csv file.
EmployeeCSVDataOutput

Save CSV file without index column

If you don’t want to have index column, you can simply pass index=False to to_csv() function as below:

When you run the program again, you will get below output:
CSVFileDataWithoutIndex

Save CSV file with Custom columns header

You can change columns header by passing header list to to_csv() as below:

When you run the program again, you will get below output:
EmployeeCSVCustomColumns
In preceding output, you can see that columns header is changed as per the header list.

Save CSV file with selected columns

To write selected columns to csv, you can pass columns list to function to_csv() as below:

When you run the program again, you will get below output:
EmployeeLimitedCol
In preceding output, you can see that Name and Age columns were saved to CSV file.

Save CSV file with custom delimiter

You can also pass custom delimiter with sep attribute as argument.
Let’s say you want to use ‘|’ as a separator, you can do as below:

EmployeePiPeDelData

Save CSV file without header

You can also pass header=False to save CSV file without header.
Let’s say you want to use ‘|’ as a separator, you can do as below:

Output:
CSVFileWithoutHeader
In preceding output, you can see that CSV file does not contain header.

Append data to CSV file

You can also append data to CSV file using mode='a' and pass header=False, otherwise header will be repeated in append mode.
Let’s see with the help of example:

EmployeeCSVApp
In preceding output, you can see that new row is appended to csv file.

Save CSV file with index_label

If you want to assign name to index column, you can pass index_label to to_csv() as below:

Output:
EmployeeCSVWithSerialNo
In preceding output, you can see that index column has heading named Serial no.

That’s all about Pandas DataFrame to CSV.

Previous
Next

Add Comment