How to Convert Pandas DataFrame into a List?
Last Updated :
29 Nov, 2023
In this article, we will explore the process of converting a Pandas DataFrame into a List, We’ll delve into the methods and techniques involved in this conversion, shedding light on the versatility and capabilities of Pandas for handling data structures in Python.
Ways to convert Pandas DataFrame Into a List
There are various methods of converting Pandas DataFrame into a List, we are discussing some generally used methods for converting Pandas DataFrame into a List with an example:
- Pandas DataFrame Column to List
- Pandas DataFrame into Nested List
- Pandas DataFrame into Nested List of columns
- Pandas DataFrame to List with Column Names
Creating a Dataframe in Pandas
First, let’s create a Basic DataFrame:
Python3
import pandas as pd
data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ],
'Age' : [ 35 , 70 , 45 , 20 ] }
df = pd.DataFrame(data)
df
|
Output :
Name Age
0 Tony 35
1 Steve 70
2 Bruce 45
3 Peter 20
Converting Dataframe into List
At times, you may need to convert your pandas dataframe to List. To accomplish this task, ‘ tolist() ‘ function can be used. Below is a basic example to use this function and convert the required DataFrame into a List.
Output :
[['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
Here, Each inner list contains all the columns of a particular row.
Pandas DataFrame can be converted into lists in multiple ways. Let’s have a look at different ways of converting a DataFrame one by one.
Converting Pandas DataFrame Column to List
In this example we convertes column to list below code utilizes Pandas to create a DataFrame from a dictionary containing ‘Name’ and ‘Age’ columns. It then converts the ‘Name’ column of the DataFrame into a list named ‘names’ using the tolist()
method. Finally, the code prints the resulting list of names.
Python3
import pandas as pd
data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] ,
'Age' : [ 35 , 70 , 45 , 20 ] }
df = pd.DataFrame(data)
names = df[ 'Name' ].tolist()
print (names)
|
Output:
['Tony', 'Steve', 'Bruce', 'Peter']
Converting Pandas DataFrame into Nested List
In this example we converted DataFrame to Nested List below code uses Pandas to create a DataFrame from a dictionary with ‘Name’ and ‘Age’ columns. It initializes an empty list named ‘res’ and iterates through each column of the DataFrame. For each column, the code stores the values of the column in a temporary list (‘li’) and appends this list to ‘res.’ Finally, the code prints the resulting list, which contains lists representing the values of each column in the DataFrame.
Python3
import pandas as pd
data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] ,
'Age' : [ 35 , 70 , 45 , 20 ] }
df = pd.DataFrame(data)
res = []
for column in df.columns:
li = df[column].tolist()
res.append(li)
print (res)
|
Output:
[['Tony', 'Steve', 'Bruce', 'Peter'], [35, 70, 45, 20]]
Converting Pandas DataFrame into Nested List of columns
In this example we uses df.values.tolist() method for convert dataframe to list as below code utilizes Pandas to create a DataFrame from a dictionary containing ‘Name’ and ‘Age’ columns. It then employs the ‘df.values.tolist()’ method to convert the entire DataFrame into a list (‘li’). The resulting list represents each row of the DataFrame as a sublist, and the code prints this transformed list. This concise approach streamlines the process of converting a DataFrame into a nested list of its values.
Python3
import pandas as pd
data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] ,
'Age' : [ 35 , 70 , 45 , 20 ] }
df = pd.DataFrame(data)
li = df.values.tolist()
print (li)
|
Output :
[['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
Converting Pandas DataFrame to List with Column Names
In this example we convert dataframe to list with column name as below code uses Pandas to make a table from a dictionary. It creates a list (‘li’) by putting together column names (converted to a sublist using df.columns.values.tolist()
) and the values of the table (df.values.tolist()
). So, the list has the names of the columns in the first part, and the rest of the list has the actual information in the table. The code then shows this list, giving both the names of the columns and the data in a clear way.
Python3
import pandas as pd
data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] ,
'Age' : [ 35 , 70 , 45 , 20 ] }
df = pd.DataFrame(data)
li = [df.columns.values.tolist()] + df.values.tolist()
print (li)
|
Output:
[['Name', 'Age'], ['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
Share your thoughts in the comments
Please Login to comment...