| by Arround The Web | No comments

Pandas Columns to List

Pandas library in Python has many functions and methods to work with tabular data, like DataFrame and Series. While working with Pandas, sometimes it is easier to use lists instead of Pandas objects for certain operations. We can convert the DataFrame columns into lists, perform the operations, and then convert the lists back into series or DataFrame.

This guide will present you with a detailed overview of converting Pandas columns to lists in Python.

How to Convert the Pandas Columns to Lists in Python?

The following methods are utilized in Python to convert the Pandas columns to lists:

Method 1: Convert the Pandas Columns to Lists Using “Series.values.tolist()” Method

The “Series.values.tolist()” method is utilized to retrieve the value of the column and convert it into the list. In the below code, the DataFrame is created using the “pandas.DataFrame()” method. Next the “df.values.tolist()” method converts the “Name” column value into a list and displays them to the output via print() function:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Anna', 'Lily'], 'Age': [25, 22, 32], 'Height': [4.5, 5.5, 6.3]})
print(df, '\n')

print(df['Name'].values.tolist())

 

The specified column of DataFrame has been converted into the list:

We also convert all the columns of DataFrame into a list using the “for” loop and “df.values.tolist()” method. In the below code, we first iterate over the “df.columns” attribute which contains all columns, and then convert them into a list using the “df.tolist()” method:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Anna', 'Lily'], 'Age': [25, 22, 32], 'Height': [4.5, 5.5, 6.3]})
print(df, '\n')
for column in df.columns:
    print(df[column].values.tolist())

 

All the columns of DataFrame have been converted into the list:

Besides converting column values to list we can also retrieve all the column names of the DataFrame in the list. Here in this code, the “df.columns” attribute is used along with the “tolist()” method to get all the column names of DataFrame:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Anna', 'Lily'], 'Age': [25, 22, 32], 'Height': [4.5, 5.5, 6.3]})
print(df, '\n')

print(df.columns.tolist())

 

All the column names of the DataFrame have been retrieved successfully:

Method 2: Convert the Pandas Columns to Lists Using the “list()” Function

The “list()” method can also be used to convert the Pandas columns into the list. In the below code, the “df[age]” expression is used to select the Pandas column value. This selected column value is passed to the “list()” function to convert them into the list:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Anna', 'Lily'], 'Age': [25, 22, 32], 'Height': [4.5, 5.5, 6.3]})
print(df, '\n')

print(list(df["Age"]))

 

The specified column value has been converted into the list:

Method 3: Convert the Pandas Columns to Lists Using the “Square Bracket”

The simple “[ ]” square bracket can also be utilized in Python to retrieve the list of the DataFrame columns. Firstly, the “df[age]” expression is used to select the specified column value and then we convert them into a list using the square bracket expression “[ ]”. This example code demonstrates this method:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Anna', 'Lily'], 'Age': [25, 22, 32], 'Height': [4.5, 5.5, 6.3]})
print(df, '\n')
res = [df['Age']]
print(res)
print(type(res))

 

The above code executed and retrieved the following list of columns:

Bonus: Convert the Pandas Columns to Numpy Array Lists Using the “df.to_numpy()” Method

The “DataFrame.to_numpy()” method is utilized to convert/transform the DataFrame of Pandas to a NumPy array. The array can also be represented similarly to the list with slight differences in Python. Here is a code that converts the particular columns to a Numpy array in Python:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Anna', 'Lily'], 'Age': [25, 22, 32], 'Height': [4.5, 5.5, 6.3]})
print(df, '\n')

print(df['Height'].to_numpy())

 

The below output shows the Numpy list array of the specified input column of DataFrame:

Conclusion

The “Series.values.tolist()”, “list()” and “Square Bracket” notation methods are used to convert the Pandas columns into a list. The “for” loop can also be utilized with the “df.columns” attribute to convert all the columns of DataFrame into the list. This article delivered a detailed guide on converting single or multiple columns to a list using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply