Pandas DataFrame to CSV

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.

NameAgeGender
Mary36Female
John29Male
Martin31Male

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.

Related Posts

  • Copy DataFrame in Python
    10 July

    Copy DataFrame in Pandas

    This articles provide different ways to copy DataFrame in Pandas.

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

  • 20 September

    Reorder the columns of pandas dataframe in Python

    Table of ContentsUsing reindex methodUsing column selection through column nameUsing column selection through column index In this post, we will see 3 different methods to Reordering the columns of Pandas Dataframe : Using reindex method You can use DataFrame’s reindex() method to reorder columns of pandas DataFrame. You need to pass columns=[$list_of_columns] to reindex() method […]

  • 08 September

    Pandas create Dataframe from Dictionary

    Table of ContentsUsing a Dataframe() method of pandas.Using DataFrame.from_dict() method. In this tutorial, We will see different ways of Creating a pandas Dataframe from Dictionary . Using a Dataframe() method of pandas. Example 1 : When we only pass a dictionary in DataFrame() method then it shows columns according to ascending order of their names […]

  • 08 September

    Pandas Convert list to DataFrame

    In this tutorial, We will see different ways of Creating a pandas Dataframe from List. You can use Dataframe() method of pandas library to convert list to DataFrame. so first we have to import pandas library into the python file using import statement. So let’s see the various examples on creating a Dataframe with the […]

  • 26 May

    Pandas convert column to float

    Table of ContentsConvert String column to float in PandasUsing asType(float) methodUsing toNumeric() methodConvert Integer column to float in PandasUsing asType(float) methodUsing toNumeric() method In this post, we will see how to convert column to float in Pandas. Convert String column to float in Pandas There are two ways to convert String column to float in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.