How to Export Pandas Series to a CSV File

Depending on your needs, you may use either of the 4 approaches below in order to export Pandas Series to a CSV file:

(1) No index and no header

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=False, header=False)

(2) No index but with header

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.rename('header name', inplace=True)
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=False, header=True)

(3) With index but no header

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=True, header=False)

(4) With index and header

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.rename('header name', inplace=True)
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=True, header=True)

Let’s now see the steps to apply each of the above approaches using a simple example.

Steps to Export Pandas Series to a CSV File

Step 1: Create Pandas Series

To start, create the Pandas Series that you’d like to export to CSV.

For example, let’s create a simple Pandas Series that will contain the names of 5 products:

import pandas as pd

ser = pd.Series(['computer', 'keyboard', 'printer', 'monitor', 'tablet'])
print(ser)

Run the code in Python, and you’ll get the following Series:

0    computer
1    keyboard
2     printer
3     monitor
4      tablet

Step 2: Capture the Path for the CSV File

Next, capture the path where the CSV file will be stored.

For instance, let’s suppose that the CSV file will be stored under the following path:

r'C:\Users\Ron\Desktop\Products.csv'

Where:

  • r is the character that you should place before the path name (to take care of any special characters within the path name, such as the backslash character).
  • C:\Users\Ron\Desktop\ is the actual path where the CSV file will be stored
  • Products is the file name to be created
  • .csv is the file extension for CSV files

Step 3: Export Pandas Series to CSV

You can now use the following template to export the Pandas Series to a CSV file (with no index and no header):

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=False, header=False)

For our example, the code would look as follows:

import pandas as pd

ser = pd.Series(['computer', 'keyboard', 'printer', 'monitor', 'tablet'])
ser.to_csv(r'C:\Users\Ron\Desktop\Products.csv', index=False, header=False)

Run the code in Python (adjusted to your path), and you’ll get the following CSV file with the Series data:

computer
keyboard
printer
monitor
tablet

Additional Scenarios of Exporting Pandas Series to CSV

No index but with header

You may now use this template to export the Series with no index, but with a header:

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.rename('header name', inplace=True)
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=False, header=True)

Notice that you’ll need to insert another component to the code to specify the name of the header. For instance, let’s add the header of ‘product_name‘ to represent the column name for the series.

Here is the complete code:

import pandas as pd

ser = pd.Series(['computer', 'keyboard', 'printer', 'monitor', 'tablet'])
ser.rename('product_name', inplace=True)
ser.to_csv(r'C:\Users\Ron\Desktop\Products.csv', index=False, header=True)

As you can see, the new CSV file would now have the column name without the index:

product_name
computer
keyboard
printer
monitor
tablet

With index but no header

You may now use the template below to export the Series with an index but no header:

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=True, header=False)

In this case, there is no need to specify the name of the header. Simply set header=False:

import pandas as pd

ser = pd.Series(['computer', 'keyboard', 'printer', 'monitor', 'tablet'])
ser.to_csv(r'C:\Users\Ron\Desktop\Products.csv', index=True, header=False)

You’ll now see the exported Series with the index:

0,computer
1,keyboard
2,printer
3,monitor
4,tablet

With index and header

For the final scenario, apply this template to export the series with both the index and the header

import pandas as pd

ser = pd.Series(['value_1', 'value_2', 'value_3', ...])
ser.rename('header name', inplace=True)
ser.to_csv(r'Path to store the CSV file\File Name.csv', index=True, header=True)

Here is the complete code:

import pandas as pd

ser = pd.Series(['computer', 'keyboard', 'printer', 'monitor', 'tablet'])
ser.rename('product_name', inplace=True)
ser.to_csv(r'C:\Users\Ron\Desktop\Products.csv', index=True, header=True)

As you can see, both the index and the header would appear in the CSV file:

,product_name
0,computer
1,keyboard
2,printer
3,monitor
4,tablet

You may check the following tutorial for the complete steps to export Pandas DataFrame to a CSV file.

Finally, you may wish to visit the Pandas Documentation to read more about exporting a Series to a CSV file.