Program to Convert List to CSV in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Comma Separated Values or CSV is a simple file format used to store data (numbers and text) in tabular form. CSV uses a comma as a field separator to separate different fields in each row of a CSV file.

We can write a program to convert the list to CSV in Python. There are mainly three ways to do so: using a CSV module, with the help of Pandas DataFrame, and also through a NumPy array.

Converting List to CSV in Python

The list is a linear data structure in python. It's just like a dynamically sized array in other programming languages like Java, and C++.

And CSV as we discussed above, is a simple file format to represent data in tabular form. CSV data is more comprehensive and easy to understand.

Now, let's see different methods to convert a list to CSV in python.

Method 1: Using CSV Module

We can convert a list to CSV in python easily by importing an in-built CSV module in python. In this method, we use file handling in python to read the data of a list and then write it into a CSV file.

Let's take an example to understand this better.

Code:

Output:

Explanation

In this example, the first we import the CSV module in our python program file, and then only we can use its csv.writer() method.

After importing the CSV module we create two lists:

  • fields(column)
  • rows

The fields list is used to define the columns of the CSV file, and the rows variable in the code is a list of lists that will create rows of the CSV file. Then we make use of the with statement to open EmployeeData.csv, and if no such file exists in the present directory then this command will create an EmployeeData.csv file.

After this, we write our list data(both columns/fields and rows) into the CSV file by passing the file into the writer object constructor.

Method 2: Using Pandas

In this method, we use pandas, a python package, to convert the list to CSV. We first, convert the list to the dataframe data structure and then use the to_csv() function to convert the dataframe to a CSV file.

Code:

Output:

Explanation

In the example above, we first imported the pandas library into our code and then defined the three lists, these are rows. And then, we map these rows with their columns using a dictionary. After that, we utilize the pd.DataFrame() function to convert the lists into the DataFrame and then we use the to_csv() function to convert the DataFrame data structure to a CSV file.

The DataFrame is a data structure that allows us to perform various operations through its methods. One of those is the to_csv() method that allows us to write its contents into a CSV file as we have done in the above example.

Method 3: Using Numpy

NumPy is another very famous package of python. It's a very well-known thing about Data Science and Machine Learning folks.

We can convert the list to a CSV file by using NumPy’s savetxt() function. The savetxt() function save an array to a text file. We can also use the savetxt() function to save the data into a CSV file.

Code:

Output:

Explanation In this method, we are using the NumPy package, so we first import that package in our Python code. Then like we did in other examples, we'll create our list that contains rows.

After that, we write this data in our CSV file using the np.savetxt() method that takes the CSV file name as an argument. If the file is not already present, the function will create a file and then write the list data in it.

Conclusion

It's time for the wrap folks.

  • We have discussed all the major methods used to convert lists to CSV in python.
    • Using CSV module
    • Using Pandas
    • Using NumPy
  • Comma Separated Values or CSV is a frequently used file format; hence knowing how to convert a list to a CSV file might come in handy in some tasks.
  • Pandas provides DataFrame data structure for tabular data and has built-in methods like read_csv() and to_csv() for reading and writing CSV files.
  • NumPy is a numerical computing library in Python that provides various methods to read and write CSV files, including genfromtxt() and savetxt().