How to Write Pandas Series to CSV (With Example)


Often you may want to write a pandas Series to a CSV file.

The easiest way to do so is by using the to_csv()  function, which uses the following syntax:

Series.to_csv(path, sep=’ , ‘ , na_rep= ‘ ‘, float_format=None, columns=None, …)

where:

  • path: A string that represents the path or location to output the CSV file to
  • sep: The delimiter to use in the output file (comma is default)
  • na_rep: Missing data representation
  • float_format: Format string for floating point numbers
  • columns: The columns to write to the CSV file

Note that the only argument that is required to use is the path argument, which specifies the location to send the CSV file along with the name to give the exported file.

All other arguments are optional and may be used to specify how the CSV file should be exported.

The following example shows how to use the to_csv() function in practice to export a pandas Series to a CSV file.

Example: How to Write Pandas Series to CSV File

Suppose we have a pandas Series named my_series that contains the following values:

import pandas as pd

#create Series
my_series = pd.Series([10, 14, 14, 13, 19, 25, 24, 29, 40, 12])
#view Series
print(my_series)

0    10
1    14
2    14
3    13
4    19
5    25
6    24
7    29
8    40
9    12
dtype: int64

Notice that the series named my_series contains 10 total values.

Suppose that we would like to write this Series out to a CSV file.

We can use  the to_csv() function to do so with the following syntax:

#write Series to CSV file
my_series.to_csv('my_series.csv')

If there are no errors when exporting the file, then you will receive no output as a result.

Since we don’t see any errors in the output, we can assume that the Series has been successfully exported to a CSV file named my_series.csv.

If we navigate to where this CSV file has been exported then we can view the contents of the file:

Notice that the CSV file contains all of the values from the pandas Series along with the index values, which range from 0 to 9 in this particular Series.

Note that we could also specify header=False in order to avoid outputting any header line to the file.

We can use the following syntax to do so:

#write Series to CSV file with no header
my_series.to_csv('my_series.csv', header=False)

Since we don’t receive any error when exporting the file, we can assume that the Series has been successfully exported to the CSV file.

If we navigate to where we exported the file, we can view the contents:

Notice that this CSV file contains the same data as the previous file expect that it contains no header row.

By specifying header=False, we avoid writing a header to the file. In most cases, you will likely use this argument because there is no need for a header for a pandas Series.

Note: You can find the complete documentation for the to_csv() in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas involving importing and exporting data:

How to Read CSV Files with Pandas
How to Read Excel Files with Pandas
How to Export a Pandas DataFrame to Excel
How to Export NumPy Array to CSV File

Featured Posts

Leave a Reply

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