Python 3 Script to Export Pandas DataFrame and Download as CSV File Full Project For Beginners

You are currently viewing Python 3 Script to Export Pandas DataFrame and Download as CSV File Full Project For Beginners

 

Welcome folks today in this blog post we will be exporting pandas dataframe data to a csv file in python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip  command as shown below

 

pip install pandas

 

After installing this library you need to make an app.py file and copy paste the following code

 

First of all we will be importing some data into our pandas dataframe as shown below and printing it

 

app.py

 

import pandas as pd

cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000]
        }

df = pd.DataFrame(cars, columns= ['Brand', 'Price'])

print (df)

 

So now just run the python script

 

python app.py

 

 

 

Saving Pandas Dataframe as CSV File

 

 

Now we will be converting the pandas tabular dataframe to csv file inside the root directory as shown below

 

app.py

 

We will be using this snippet of code as shown below

 

df.to_csv("filename.csv")

 

import pandas as pd

cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000]
        }

df = pd.DataFrame(cars, columns= ['Brand', 'Price'])

print (df)

df.to_csv("output.csv")

 

 

So now you can see we have a output.csv file created inside the directory as shown below

 

Leave a Reply