Pandas DataFrame to CSV

Pandas DataFrame to CSV

Pandas is a powerful data manipulation library in Python. It provides a flexible and efficient DataFrame object, which is a two-dimensional labeled data structure with columns of potentially different types. One of the most common tasks when working with an analysis library such as Pandas is exporting your data into a variety of different output formats. In this article, we will focus on how to convert a Pandas DataFrame to a CSV file.

Creating a DataFrame

Before we can export a DataFrame to a CSV file, we first need to create a DataFrame. Here is an example of how to create a DataFrame from a dictionary.

import pandas as pd

data = {
    'Name': ['John', 'Anna', 'Peter', 'Linda'],
    'Age': [28, 24, 35, 32],
    'City': ['New York', 'Paris', 'Berlin', 'London']
}

df = pd.DataFrame(data)

print(df)

Output:

Pandas DataFrame to CSV

Exporting a DataFrame to a CSV File

Pandas provides the to_csv function to write a DataFrame to a local CSV file on your system. Here is a basic example:

df.to_csv('pandasdataframe.com.csv')

By default, this function will write the DataFrame to a CSV file with the same column order as the DataFrame, and it will include the DataFrame’s index.

Specifying the File Path

You can specify the file path where you want to save the CSV file. If you don’t specify a path, the file will be saved in the current working directory.

df.to_csv('/path/to/pandasdataframe.com.csv')

Excluding the Index

By default, the to_csv function includes the DataFrame’s index. If you don’t want to include the index, you can set the index parameter to False.

df.to_csv('pandasdataframe.com.csv', index=False)

Specifying the Column Order

You can specify the column order in the CSV file by passing a list of column names to the columns parameter.

df.to_csv('pandasdataframe.com.csv', columns=['Name', 'City', 'Age'])

Specifying the Separator

By default, the to_csv function uses a comma as the separator. You can specify a different separator by setting the sep parameter.

df.to_csv('pandasdataframe.com.csv', sep='\t')

Writing to a Compressed CSV File

You can write to a compressed CSV file by specifying the compression parameter.

df.to_csv('pandasdataframe.com.csv.gz', compression='gzip')

Specifying the Encoding

You can specify the encoding of the CSV file by setting the encoding parameter.

df.to_csv('pandasdataframe.com.csv', encoding='utf-8')

Writing to a CSV File in Chunks

If your DataFrame is very large, you can write to a CSV file in chunks by specifying the chunksize parameter.

df.to_csv('pandasdataframe.com.csv', chunksize=1000)

Specifying the Date Format

You can specify the date format in the CSV file by setting the date_format parameter.

df.to_csv('pandasdataframe.com.csv', date_format='%Y-%m-%d')

Handling Missing Data

By default, the to_csv function writes NaN for missing data. You can specify a different value by setting the na_rep parameter.

df.to_csv('pandasdataframe.com.csv', na_rep='UNKNOWN')

Pandas DataFrame to CSV Conclusion

In this article, we have learned how to export a Pandas DataFrame to a CSV file. We have seen how to specify the file path, exclude the index, specify the column order, specify the separator, write to a compressed CSV file, specify the encoding, write to a CSV file in chunks, specify the date format, and handle missing data. With these techniques, you should be able to export your DataFrame to a CSV file in any way you need.

Like(0)