Open In App

Python – Save List to CSV

Improve
Improve
Like Article
Like
Save
Share
Report

A CSV (Comma Separated Values) is a simple file format, used to store data in a tabular format. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. There are various methods to save lists to CSV which we will see in this article.

Python List to CSV Using CSV Module 

The code uses the csv module to write data into a CSV file named ‘GFG’. It defines the field names as [‘Name’, ‘Branch’, ‘Year’, ‘CGPA’] and the data rows as a list of lists. It opens the file in write mode and uses the csv.writer method to write the field names as the first row and then writes the data rows into the file.

Python3




import csv
 
 
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
   
# data rows of csv file
rows = [ ['Nikhil', 'COE', '2', '9.0'],
         ['Sanchit', 'COE', '2', '9.1'],
         ['Aditya', 'IT', '2', '9.3'],
         ['Sagar', 'SE', '1', '9.5'],
         ['Prateek', 'MCE', '3', '7.8'],
         ['Sahil', 'EP', '2', '9.1']]
 
with open('GFG', 'w') as f:
     
    # using csv.writer method from CSV package
    write = csv.writer(f)
     
    write.writerow(fields)
    write.writerows(rows)


Output: 

 

 Python List to CSV Using Pandas 

The code imports the pandas library as pd. It defines three lists: nme for names, deg for degrees, and scr for scores. It creates a dictionary dict using these lists. Then, it creates a pandas DataFrame df from the dictionary. Finally, it saves the DataFrame as a CSV file named ‘GFG.csv’ using the to_csv method. The resulting CSV file will contain the columns ‘name’, ‘degree’, and ‘score’ with the corresponding data from the lists.

Python3




# importing 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]
     
# dictionary of lists
dict = {'name': nme, 'degree': deg, 'score': scr}
     
df = pd.DataFrame(dict)
     
# saving the dataframe
df.to_csv('GFG.csv')


Output: 

 

List to CSV Using Numpy 

The code imports the numpy library as np. It defines a list of lists rows representing the data rows of a CSV file. It then uses the np.savetxt function to save the data as a CSV file named ‘GFG.csv’. The delimiter parameter specifies the delimiter to use between values (“, ” in this case), and the fmt parameter specifies the format of each value (“%s” indicating a string format). The resulting CSV file will contain the data rows with values separated by commas and spaces.

Python3




import numpy as np
 
 
# data rows of csv file
rows = [ ['Nikhil', 'COE', '2', '9.0'],
        ['Sanchit', 'COE', '2', '9.1'],
        ['Aditya', 'IT', '2', '9.3'],
        ['Sagar', 'SE', '1', '9.5'],
        ['Prateek', 'MCE', '3', '7.8'],
        ['Sahil', 'EP', '2', '9.1']]
 
# using the savetxt
# from the numpy module
np.savetxt("GFG.csv",
        rows,
        delimiter =", ",
        fmt ='% s')


Output: 

 



Last Updated : 26 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads