Python Pandas

Pandas to CSV

While working with pandas DataFrames, in some scenarios, you may need to store the data present in the DataFrame locally. This data can be utilized for some other purposes like analysis, visualization etc. The best way to export the data from an existing pandas DataFrame into CSV file is by using the pandas.DataFrame.to_csv() function supported by the pandas module. In this guide, we will discuss how to export the data from an existing DataFrame to a CSV file with examples. In each example, we will include different parameters that allow you to export CSV file with or without row index and headers.

pandas.DataFrame.to_csv

The pandas.DataFrame.to_csv method writes the specified DataFrame object to a CSV (Comma Separated Values) file. We will look at the syntax of this function and discuss different parameters passed to this function.

Syntax

It is necessary to specify the pandas DataFrame as an argument for the function. The data in the pandas DataFrame is stored as a string if no parameter is specified.

InputDataFrame_obj.to_csv(file_name/path_to_file, sep=',', columns=None, header=True, index=True, index_label=None, mode='w', compression='infer')

Parameters

  1. The file_name/path_to_file specifies the source where the CSV file has to be stored on your local PC. The data present in the pandas DataFrame stored in the CSV with the specified filename.
  2. The sep is an optional parameter that is used as a separator. The values in each row are separated by the comma delimiter by default.
  3. The columns: if you want to export only specific columns from the pandas DataFrame into the CSV, you can pass the columns to this parameter through the List.
  4. Column names can be included or excluded by specifying the header parameter. The column names will be included in the CSV file by default. You need to pass the boolean value – False, if you don’t want the column names in the exported CSV file.
  5. Row indices can be included or excluded by specifying the index parameter. The row indices will be included in the CSV file by default. You need to pass the boolean value – False, if you don’t want them in the exported CSV file.
  6. If you include the row indices (index=True), there is an option to specify a name to your index column. index_label parameter is used in this scenario. We need to assign the label to this parameter.
  7. The exported data is in write mod, so the mode will be “w”.
  8. compression – compress the output file in different formats like ‘zip’, ‘gzip’, ‘bz2’, ‘zstd’, ‘tar’ etc.

Sample DataFrame

In this entire guide, we will consider this pandas DataFrame in all examples. So, make sure that you have implemented the below code in your Python environment before implementing all the examples. df_from_campaign_data is the name of the DataFrame that holds 4 columns: [‘Campaign_Name’, ‘Type’, ‘Status’, ‘Budget’] and five records.

import pandas

# Create campaign_data with 5 records
campaign_data = [['LinuxHint Marketing camp','Conference','Planned',25000],
                  ['LinuxHint Sales camp','Conference','Completed',5000],
                  ['LinuxHint Tutorial camp','Webinar','In-Progress',10000],
                  ['LinuxHint Services','Trade-Show','Completed',2000],
                  ['LinuxHint Python Bootcamp','Webinar','Planned',1000]]

# Pass the above data to the pandas DataFrame and
# specify the column names.
df_from_campaign_data = pandas.DataFrame(campaign_data,columns=['Campaign_Name','Type','Status','Budget'])
print(df_from_campaign_data)

Output

Example 1: Exporting as String

Let’s export the above pandas DataFrame as a string. We will not pass any parameter to the to_csv() function.

# Write the above DataFrame to string
print(df_from_campaign_data.to_csv(),"\n")

# Check the type
print(type(df_from_campaign_data.to_csv()))

Output

Entire DataFrame is exported as a string and you can also see the type of it.

Example 2: Exporting as CSV

Let’s export the above pandas DataFrame to a CSV file – Camps.csv. Here we will pass only the filename.

# Export df_from_campaign_data to CSV
df_from_campaign_data.to_csv('Camps.csv')

Output:

DataFrame is exported to CSV with headers (Columns) and Row Indices (Index).

Example 3: With index_label as the Parameter

In Example 2, we saw the index column had no column name. Let’s specify the column name as ‘Camp ID’ using the index_label parameter.

# Specify the label for Index column
df_from_campaign_data.to_csv('Camps.csv',index=True,index_label='Camp ID')

Output

Now you can see the column name ‘Camp ID’ for the row indices.

Example 4: With header & index as Parameters

By default, we can see the column names and indices. Let’s remove them by setting header and index to False.

# Exclude the column names and index in the CSV
df_from_campaign_data.to_csv('Camps.csv',header=False,index=False)

Output:

Column names and indices no longer exist in the below CSV file.

Example 5: With sep Parameter

Let’s export to CSV with tab separated space using the sep parameter.

# Export the DataFrame to CSV with tab separated - ‘\t’
df_from_campaign_data.to_csv('Camps.csv',sep='\t')

# By default it will be comma separated - ‘,’
df_from_campaign_data.to_csv('Camps.csv',sep=',')

Output

View both the CSV outputs.

Example 6: With compression Parameter

Let’s export to CSV with Zip. compression parameter takes the dict() with method – zip and Camps.csv as archive_name.

# Export the CSV as a Zip file
df_from_campaign_data.to_csv('Camps.zip',compression=dict(method='zip',archive_name='Camps.csv'))

Output

You can see the CSV file under the Camps.zip. Extract All to unzip it and view the CSV file.

Conclusion

We learned to export the existing pandas DataFrame to CSV using the pandas.DataFrame.to_csv function. Most of the parameters that are passed to this function are discussed with examples and output screenshots. Before implementing all the examples, create your own pandas DataFrame and then start implementing the examples provided in this guide.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain