How to Convert Pandas DataFrame into a List?
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 # Creating a dictionary to store data data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ], 'Age' : [ 35 , 70 , 45 , 20 ] } # Creating DataFrame df = pd.DataFrame(data) # Print the dataframe 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.
Python3
df.values.tolist() |
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 # Creating a dictionary to store data data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] , 'Age' : [ 35 , 70 , 45 , 20 ] } # Creating DataFrame df = pd.DataFrame(data) # Converting DataFrame to a list containing # all the rows of column 'Name' names = df[ 'Name' ].tolist() # Printing the converted list. 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 # Creating a dictionary to store data data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] , 'Age' : [ 35 , 70 , 45 , 20 ] } # Creating DataFrame df = pd.DataFrame(data) # Creating an empty list res = [] # Iterating through the columns of # dataframe for column in df.columns: # Storing the rows of a column # into a temporary list li = df[column].tolist() # appending the temporary list res.append(li) # Printing the final list 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 # Creating a dictionary to store data data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] , 'Age' : [ 35 , 70 , 45 , 20 ] } # Creating DataFrame df = pd.DataFrame(data) # Converting dataframe to list li = df.values.tolist() # Printing list 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 # Creating a dictionary to store data data = { 'Name' :[ 'Tony' , 'Steve' , 'Bruce' , 'Peter' ] , 'Age' : [ 35 , 70 , 45 , 20 ] } # Creating DataFrame df = pd.DataFrame(data) # Converting dataframe to list li = [df.columns.values.tolist()] + df.values.tolist() # Printing list print (li) |
Output:
[['Name', 'Age'], ['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
Contact Us