Change language

Saving Pandas Dataframe as CSV

| | |

Most of the datasets you work with are named DataFrames. DataFrames — it is a two-dimensional labeled data structure with an index for rows and columns, where each cell is used to store a value of any type. Essentially, DataFrames — it is a dictionary based on NumPy arrays.

Let’s see how to save a Pandas DataFrame as a CSV file using to_csv () .

Example # 1: Save CSV to working directory.

# import pandas as pd

import pandas as pd 

 
# list of name, degree, score

nme = [ "aparna" , "pankaj" , "sudhir" , "Geeku" ]

deg =   [ "MBA" , " BCA " , " M.Tech " , "MBA" ]

scr = [ 90 , 40 , 80 , 98 ]

 
# list dictionary

dict = { ’name’ : nme, ’degree’ : deg, ’ score’ : scr} 

  

df = pd.DataFrame ( dict )

 
# save data frame

df.to_csv ( ’file1.csv’ )

Output:

Example # 2: Saving CSV without headers and index .

# import pandas as pd

import pandas as pd 

 
# list of name, degree, score

nme = [ "aparna" , "pankaj" , "sudhir" , "Geeku" ]

deg = [ "MBA" , "BCA" , "M.Tech" , " MBA " ]

scr = [ 90 , 40 , 80 , 98 ]

 
# list dictionary

dict = { ’name’ : nme, ’degree’ : deg, ’ score’ : scr} 

 

df = pd.DataFrame ( dict )

 
# save data frame

df.to_csv ( ’file2.csv’ , header = Fals e , index = False )

Output:

Example # 3: Save the CSV file to the specified place.

# import pandas as pd

import pandas as pd 

 
# list of name, degree, points

nme = [ "aparna" , "pankaj" , "sudhir" , < / code> "Geeku" ]

deg = [ "MBA" , "BCA" , "M.Tech" , "MBA" ]

scr = [ 90 , 40 , 80 , 98 ]

 
# list dictionary

dict = { ’name’ : nme, ’ degree’ : deg, ’score ’ : scr} 

  

df = pd.DataFrame ( dict )

 
# save frame data

df.to_csv (r ’C: UsersAdminDesktopfile3.csv’ , index = False )

Output:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method