| by Arround The Web | No comments

Pandas Export to CSV

Comma-separated values (CSVs) are text files that store data in rows, with each value separated by a comma. The CSV file format lets users easily transfer and convert data between various applications and formats. To save DataFrame as a comma-separated file, the pandas “to_csv()” method is utilized in Python.

This post presents a detailed tutorial on exporting Pandas DataFrame to CSV using numerous examples.

How to Export Pandas DataFrame to CSV?

The “DataFrame.to_csv()” method of the Pandas module is utilized to export DataFrame to comma-separated value or CSV. Let’s understand this method using the below syntax:

DataFrame.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)

Here in the above syntax:

  • The “path_or_buf” parameter is the file name or file-like object to write the CSV data. If None, the result/output will be retrieved as a string.
  • The “sep” parameter is the field delimiter for the output file.
  • The “na_rep” parameter is the missing data representation. (Default is Empty String).
  • The “float_format” and the “columns” parameters are used to specify the format string for floating point numbers and the sequence of column names to write.
  • The “header” parameter is a Boolean or a list of strings that indicates whether to write out the column names or aliases. The default is “True”, which means the column names are written.
  • The other optional parameters such as index, index_label, mode, etc., can also be utilized to execute particular tasks when passed inside the “DataFrame.to_csv()” function.

Example 1: Exporting Pandas DataFrame to CSV

To export the Pandas DataFrame to CSV, let’s have a look at the below code:

import pandas
dict1 = {'name': ['Joseph', 'Anna', 'Lily', 'Henry'], 'Age': [22, 12, 18, 22], 'Height': [4.9, 5.4, 4.8, 5.8]}
df = pandas.DataFrame(dict1)
print(df)
df.to_csv('new.csv', index=False)

Here in this code, the “pandas.DataFrame()” takes the “dict1” object as an argument and retrieves the DataFrame. After that, the “df.to_csv()” method converts the DataFrame to a comma-separated file. The “index=False” parameter value is passed to the “df.to_csv()” method to export the Pandas DataFrame to CSV without the “index” column.

Output

The DataFrame has been exported to CSV.

Example 2: Exporting Pandas DataFrame to CSV Using “header” Parameter

The following example export Pandas DataFrame to CSV without using the header of the Pandas DataFrame:

import pandas
dict1 = {'name': ['Joseph', 'Anna', 'Lily', 'Henry'], 'Age': [22, 12, 18, 22], 'Height': [4.9, 5.4, 4.8, 5.8]}
df = pandas.DataFrame(dict1)
print(df)
df.to_csv('new.csv', header=False, index=False)

In this code, the “header=False” parameter value is passed inside the “df.to_csv()” method to export Pandas DataFrame to CSV without a header (column names).

Output

The DataFrame has been exported to CSV with no header file.

Example 3: Exporting Particular Columns of DataFrame to CSV Using “columns” Parameter

The below code is used to export particular columns of DataFrame to comma-separated values:

import pandas
dict1 = {'name': ['Joseph', 'Anna', 'Lily', 'Henry'], 'Age': [22, 12, 18, 22], 'Height': [4.9, 5.4, 4.8, 5.8]}
df = pandas.DataFrame(dict1)
print(df)
df.to_csv('new.csv', index=False, columns=['Age', 'Height'])

Here, the specified column is passed to the “df.to_csv()” method using the “columns” parameter. The “df.to_csv()” method exports specific columns to CSV.

Output

As shown in the above snippet, the specified columns of Pandas DataFrame have been exported to CSV.

Conclusion

In Python, the “DataFrame.to_csv()” method of the “Pandas” module is used to export Pandas DataFrame to CSV(Comma Separated Value). This method uses “index” and “header” parameters to ignore the index and header while exporting to CSV. There are several parameters that can be specified/passed inside the “df.to_csv()” method to perform different tasks while exporting. This guide has illustrated an in-depth guide on exporting Pandas to CSV using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply