Home » Pandas Write a DataFrame to a CSV file

Pandas Write a DataFrame to a CSV file

Oracle Java Certification
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests

In this article we will take  a look at how you can write a DataFrame to a csv file

The first step is to create a DataFrame with a few rows and columns filled with data

import pandas as pd

population = {
    'Country':["Cameroon","Canada","Colombia","France"],
    'Capital':["Yaounde","Ottawa","Bogota","Paris"],
    'Continent':["Africa","North America","South America","Europe"],
    'Population':[27914536,38454327,51874024,64626628]
          }
df = pd.DataFrame(population)

Syntax

# to_csv() 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='"', line_terminator=None, 
chunksize=None, date_format=None, doublequote=True, escapechar=None, 
decimal='.', errors='strict', storage_options=None)

 

Now let's create a CSV file

DataFrame to CSV File

Pandas DataFrame provides the to_csv() function to write/export DataFrame to a CSV  file along with a header and index.

import pandas as pd

population = {
    'Country':["Cameroon","Canada","Colombia","France"],
    'Capital':["Yaounde","Ottawa","Bogota","Paris"],
    'Continent':["Africa","North America","South America","Europe"],
    'Population':[27914536,38454327,51874024,64626628]
          }
df = pd.DataFrame(population)
print(df)
df.to_csv("countries.csv")

This creates a countries csv file and it has the following contents.

,Country,Capital,Continent,Population
0,Cameroon,Yaounde,Africa,27914536
1,Canada,Ottawa,North America,38454327
2,Colombia,Bogota,South America,51874024
3,France,Paris,Europe,64626628

Write DataFrame to CSV without Header

You can use the header=False param to write a DataFrame without a header . By default to_csv() method exports DataFrame to a CSV file with a header

import pandas as pd

population = {
    'Country':["Cameroon","Canada","Colombia","France"],
    'Capital':["Yaounde","Ottawa","Bogota","Paris"],
    'Continent':["Africa","North America","South America","Europe"],
    'Population':[27914536,38454327,51874024,64626628]
          }
df = pd.DataFrame(population)
print(df)
df.to_csv("countries1.csv", header=False)

 

Use a Custom Delimiter

By default CSV file is created with a comma delimiter, you can change this behavior by using sep param (separator) and chose other delimiters

import pandas as pd

population = {
    'Country':["Cameroon","Canada","Colombia","France"],
    'Capital':["Yaounde","Ottawa","Bogota","Paris"],
    'Continent':["Africa","North America","South America","Europe"],
    'Population':[27914536,38454327,51874024,64626628]
          }
df = pd.DataFrame(population)
print(df)
df.to_csv("countries2.csv", header=False, sep='|')

Export Selected Columns

Sometimes you may need to export selected columns from DataFrame to CSV File, In order to select specific columns we can use the columns param.

In this example, I have created a list called column_names with the required columns that I want to export

import pandas as pd

population = {
    'Country':["Cameroon","Canada","Colombia","France"],
    'Capital':["Yaounde","Ottawa","Bogota","Paris"],
    'Continent':["Africa","North America","South America","Europe"],
    'Population':[27914536,38454327,51874024,64626628]
          }
df = pd.DataFrame(population)
print(df)
# Export Selected Columns to CSV File
column_names = ['Country', 'Capital','Population']
df.to_csv("countries3.csv",index=False, columns=column_names)

 

Links

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

https://github.com/programmershelp/maxpython/tree/main/pandas/writecsv

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More