Python

Pandas Columns to List

“In Python, a list is an ordered sequence of items that can be changed or modified. A list’s items are any elements or values that are contained within it. We can define a list by inserting the values inside square brackets [], just as strings are defined by characters inside quotations. There may be situations when we have to perform such operations that can be performed better with lists only rather than a pandas object like series and dataframe. In these circumstances, we can perform the necessary operations to change the DataFrame columns in a list. The list can then be changed again into the series or dataframe.”

How to Convert Pandas Columns to Lists?

There are several ways to convert the pandas columns into lists. We will implement a few of them to show you how we can convert columns to lists using different functions in pandas.

Example # 1: Using Series.tolist to Convert Pandas Columns to a List

We will create a dataframe first, from which we will select a column to convert that column into a list.


We have imported the pandas module to use its functionalities. Then we created a dictionary to store the data. We created three keys, “Name” consisting of the string value, “Age”, and “Salary” consisting of numeric values. We have passed this dictionary to a variable “d”. To create the dataframe from the “d” dictionary, we have passed it as an argument inside the pd.DataFrame() function. To view our dataframe, we can use the print function.


Our dataframe is created with three columns. In the “Name” column, we have names of persons (“Tom”, “Ben”, “Sam”, “Mike”), the ages of each person (35, 30, 43, 40) in the “Age” column, and in the “Salary” column we have salaries of each person (12000, 14000, 13000, 12500). By using the [] operator, we will select the column by its name, which will return a Series object. The Series.tolist() function will then be used to convert the series into a list.


We have selected the column from the “df” dataframe and applied the tolist() function to convert the “Name” column into the list. After creating the list, we have assigned the list to the “d_list” variable. Now we can see the newly created list by passing the “d_list” variable inside the print() function.


So, our “Name” column is successfully converted into a list. We can also verify it by using the type() function as below:


The type() function has verified that “d_list” is a list, as seen above.

Example # 2: Utilize the numpy.ndarray.tolist() Function to Turn Columns Into Lists

This example is quite similar to example # 1. Same as the previous example, we will create a dataframe first. To create the dataframe, the pandas module will be imported as pd first. Then we will pass a dictionary in the pd.DataFrame() function as input.


As can be seen, our “fruits” column stores the string values as the name of the fruits (“mango”, “orange”, “banana”, “melon”, “peach”, “apple”). Whereas the “quantity” column has integer values representing the number of fruits (6, 12, 12, 2, 10, 10), and the “price” column stores the price of fruits (200, 150, 100, 125, 200, 180). Now we will select the column from the dataframe by using the [] operator, which produces a Series object, and use the attribute “Series.Values” to return an ndarray from the series.


We have selected the “price” column from our “df” dataframe. The “.value” attribute then converted the column into an array. After converting the “price” column to an array, we used the tolist() function to convert it into a list. Let’s use the print() function to visualize the list.


The type() function will be used to verify whether our column is converted into a list or not.


So, our “price” column from the “df” array is successfully converted to a list.

Example # 3: Converting All the Columns and Rows to a List Using the tolist() Function

We have already seen how we can convert a specific column of the dataframe to a list. In this example, we will convert all columns of rows in our dataframe to a list. Let’s use the dataframe which we created in example # 2.


Now, we will use the tolist() method on our dataframe to convert a column of rows to a list.


In the above code, we used df.values; the “df.values” is used to return a numpy array containing the data of our “df” dataframe, without any indexes or column names. Then, we use the tolist() function with df.values to convert the retrieved array into a list. We assigned this list to a variable named “r_list”.


As you can see, all the columns of rows in our dataframe are converted into lists using the df.values.tolist() function.

Example # 4: Making a Nested List Out of All the Columns and Rows Using the tolist() Function

In example # 3, the columns were converted into lists in the form of row data. We will convert all columns in our dataframe to a nested list in this example. Each list in the nested list will contain the data inside each column in our dataframe. Let’s use the same dataframe which we have created in example # 2.


Now we will create an empty list which we will use later on to store the nested lists.


There are three columns in our dataframe, i.e., “fruits”, “quantity”, and “price”. For fetching the data from each column, we will use the “for” loop. Using a “for” loop is a quick and easy approach to iterate through the columns of a pandas DataFrame. The loop will extract the data from columns. Then, inside the loop, we will convert the columns into lists and use the append() function to store the lists of columns in our “temp” list.


We used a for loop to iterate through the columns of the dataframe and fetch all the data stored in the columns of the dataframe. This loop will convert all the columns into a nested list and store it in our “temp” list.


As can be seen, each column of our dataframe is converted into a nested list.

Example # 5: Utilizing the list() Method, Create a List From the Column

The dataframe’s columns can also be converted into lists by using the list() function. To show the working of the list() function, let’s use the dataframe which we created in example # 1.


Now we will use the list() function to convert a specific column into a list. The list() function in pandas is used to create lists.


Inside the list() function, we have selected the “Name” column from our dataframe to convert it into a list. To view this created list, we will use the print() function.


The “Name” column is successfully converted into a list.

Conclusion

In this article, we have seen that we can convert the pandas columns to lists if required. After covering this tutorial, you should now be able to convert the single, multiple, or all columns of the dataframe into a list. In the examples of this tutorial, we tried to teach you how to use the tolist() function, numpy.ndarray.tolist() function, and how to use the list() function to convert the dataframe’s columns into lists.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.