| by Arround The Web | No comments

Pandas Series to CSV

In Python, while working with data analysis, we might use the popular “pandas” library. It is utilized for manipulating and exploiting data structures, such as Series and DataFrame. The one-dimensional array-like object named “Series” can hold any data type like str, int, and booleans. While working with this we sometimes need to convert it into a CSV or comma-separated value file.

This write-up will present you with a detailed overview of the Pandas Series to CSV using the following contents:

What is the “Series.to_csv()” in Python?

In Python, the “Series.to_csv()” method is utilized to write a Pandas Series object into a CSV (comma-separated values) file.

Syntax

Series.to_csv(path_or_buf=None, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', lineterminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.', errors='strict', storage_options=None)

 

Parameters

In the above syntax:

  • The “path_or_buf” parameter represents the file path where the Series will be written.
  • The “sep” parameter is the delimiter for the fields in the CSV file.
  • The “na_rep” parameter is the representation of the missing series value.
  • The “mode” parameter indicates the write mode for the file and the “columns” parameter specifies the columns to write.
  • The “header” parameter is the boolean value or list of strings specifying whether to write out the column’s name.
  • The “index” parameter specifies whether to write out the row names, and the “index_label” parameter specifies the column label for the index column.

You can review this official documentation to learn more about the syntax.

Return Value

None or str

Example 1: Using the “Series.to_csv()” Method to Export Pandas Series to CSV

In this example code, the “pd.Series()” method is utilized to create a Series object. Next, the “Series.to_csv()” method takes the CSV file path, index, and header value as an argument to export the Pandas series to CSV without index and header value:

import pandas as pd
sr = pd.Series(['Joseph', 'Anna', 'Jason', 'Jordan', 'Lily'])
sr.to_csv(r'C:\Users\p\Documents\program\example.csv', index=False, header=False)

 

The above code executed and retrieved the CSV file:

The following property of the file verified that the Series data has been converted into CSV:

Example 2: Using the “Series.to_csv()” Method to Export Pandas Series to CSV with Header

The “Series.to_csv()” method can be used to export the Pandas series to CSV with a header row by utilizing the “header=True” parameter. The name of the header is set using the “rename()” method of the series:

import pandas as pd
sr = pd.Series(['Joseph', 'Anna', 'Jason', 'Jordan', 'Lily'])
sr.rename('Name', inplace=True)
sr.to_csv(r'C:\Users\p\Documents\program\example.csv', index=False, header=True)

 

The Series has been exported to CSV with a specific header value:

Example 3: Using the “Series.to_csv()” Method to Export Pandas Series to CSV with Index

Here in this code, the “Series.to_csv()” method is used with the “index=True” parameter to export the Pandas series to a CSV file with an index column. Here is a code that demonstrates this example:

import pandas as pd
sr = pd.Series(['Joseph', 'Anna', 'Jason', 'Jordan', 'Lily'])
sr.to_csv(r'C:\Users\p\Documents\program\example.csv', index=True, header=False)

 

The Series with the index column has been exported to CSV:

Conclusion

The “Series.to_csv()” method is utilized to write a Pandas Series object with index and header to a comma-separated values (CSV) file. The “header=”, and “index=” boolean value decide whether to add index or header while exporting to CSV in Python. This blog demonstrated about exporting the Pandas Series to CSV.

Share Button

Source: linuxhint.com

Leave a Reply