Python

Pandas Columns to List

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.

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 the columns to lists using different functions in Pandas.

Method 1: Using Tolist()

The tolist() is the built-in method available in Python which converts a particular DataFrame column to a list. We need to pass the column name after the DataFrame object with the dot (.) operator.

Syntax:

DataFrame_object.column_name.tolist()

Example 1:
Let’s have a DataFrame that holds 4 columns and convert the “shops” column to a list.

import pandas
wages=pandas.DataFrame({'shops':["type 1","type 2","type 3"],
                          'rent':[12000,6700,3000],
                          'address':['city A','city C','city A'],
                          'workers':[10,10,34]})

print(wages)  

print()

# Convert workers column to list using tolist() function
converted=wages.shops.tolist()
print(converted)

# Display the type
print(type(converted))

Output:

For confirmation, we also display the type of coveted list.

Example 2: Values with Tolist()
Values is the attribute which returns the values in a DataFrame column. If we pass the tolist() after this attribute, the column is converted to a list.

Let’s convert the “address” column to a list.

import pandas
wages=pandas.DataFrame({'shops':["type 1","type 2","type 3"],
                          'rent':[12000,6700,3000],
                          'address':['city A','city C','city A'],
                          'workers':[10,10,34]})

# Convert the address column values to list using tolist()
converted=wages.address.values.tolist()
print(converted)
print(type(converted))

Output:

Now, the address column is converted to a list.

Example 3: Convert All Columns to List Using Tolist()
It can be possible to convert all the columns in the DataFrame to a list. We just need to loop each column and convert the iterator into a list using tolist().

import pandas
wages=pandas.DataFrame({'shops':["type 1","type 2","type 3"],
                          'rent':[12000,6700,3000],
                          'address':['city A','city C','city A'],
                          'workers':[10,10,34]})

# convert all columns to list one by one using tolist()
for column in wages.columns:
    print(wages[column].tolist())

Output:

There are 4 columns in the wages DataFrame. All are converted to a list. Now, there are 4 lists in the output.

Method 2: Using List()

List() is used to create a list in Python. If we provide the DataFrame column as input to this function, the column is converted to a list.

Syntax:

list(DataFrame_object.column_name)

Example 1:
Let’s have a DataFrame that holds 4 columns and convert the “rent” column to a list.

import pandas
wages=pandas.DataFrame({'shops':["type 1","type 2","type 3"],
                          'rent':[12000,6700,3000],
                          'address':['city A','city C','city A'],
                          'workers':[10,10,34]})

# Convert rent column to list
converted=list(wages.rent)

print(converted)
print(type(converted))

Output:

For confirmation, we also display the type of coveted list.

Example 2: Display All Columns in a List Using List()
Columns is the attribute which returns all the column names. If we pass this in the list(), the column names can be returned in a list.

Let’s display all the column names in a list.

import pandas
wages=pandas.DataFrame({'shops':["type 1","type 2","type 3"],
                          'rent':[12000,6700,3000],
                          'address':['city A','city C','city A'],
                          'workers':[10,10,34]})
# Display all columns in list
print(list(wages.columns))

Output:

List returns all the column labels.

Method 3: Using []

The [] is used to create a list in Python. If we provide the DataFrame column inside the [], the column is converted to a list.

Syntax:

[DataFrame_object[‘column_name’]]

Example:
Convert the “Workers” column to a list.

import pandas
wages=pandas.DataFrame({'shops':["type 1","type 2","type 3"],
                          'rent':[12000,6700,3000],
                          'address':['city A','city C','city A'],
                          'workers':[10,10,34]})

# Convert workers column to list using []
converted=[wages['workers']]
print(converted)
print(type(converted))

Output:

Conclusion

We saw that we can convert the Pandas columns to lists if required. After covering this tutorial, you should now be able to convert the single 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 and [], list() functions, and how to use the list() function to convert the DataFrame’s columns into lists.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain