How to Export Pandas Series to a CSV File

Here are 4 approaches 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)

Steps to Export Pandas Series to a CSV File

Step 1: Create a Pandas Series

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

For example, create a simple Pandas Series that contains 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, assume that the CSV file (where the file name is “Products” and the file type is “.csv“) will be stored under the following path:

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

Step 3: Export the Pandas Series to CSV

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:

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

To add the header of ‘product_name‘ to represent the column name for the Series:

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)

The result with the new column name:

product_name
computer
keyboard
printer
monitor
tablet

With index but no header

In this case, 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)

The exported Series with the index:

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

With index and header

With index and header:

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)

The result:

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

Check the following tutorial for the complete steps to export Pandas DataFrame to a CSV file.

Leave a Comment