How to Convert Pandas DataFrame into a List?

Pandas.values property is utilized to get a numpy.array and afterward utilize the tolist() capacity to change that cluster over to list. DataFrame is the two-layered information structure. DataFrame comprises of lines and segments. Information is adjusted in the even configuration. Subsequently, we can utilize the DataFrame to store the information.

Records are additionally used to store information. Notwithstanding, the rundown is an assortment that is requested and inconsistent. Records need not forever be homogeneous.

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:

df.values.tolist()

On occasion, you might have to change over your panda’s data frame to List. To achieve this errand, ‘ to list() ‘ capacity can be utilized. The following is an essential guide to utilize this capacity and convert the necessary DataFrame into a List.

Here, each internal rundown contains every one of the segments of a specific column.

Pandas DataFrame can be changed over into records in more than one way. We should examine various approaches to changing over a DataFrame individually.

Output:

[['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]

Method #1:

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']

Method #2:

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]]

Method #3:

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]]

Method #4:

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]]

Also ReadHow to Learn Programming?

Leave a Reply

Your email address will not be published.