How to Convert Pandas DataFrame into a List? - GeeksforGeeks

03 May, 2021
Let’s discuss how to convert Pandas dataframe to List. First, let’s create a Basic DataFrame:
Python3
import pandas as pd  # Creating a dictionary to store datadata = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ],        'Age': [35, 70, 45, 20] }  # Creating DataFrame df = pd.DataFrame(data)  # Print the dataframedf
 
 
Output :
 
 
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.
 
Method #1: Converting a DataFrame to List containing all the rows of a particular column:
 
Python3
import pandas as pd  # Creating a dictionary to store datadata = {'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: Converting a DataFrame to Nested List containing all the rows of all the columns:
 
Python3
import pandas as pd  # Creating a dictionary to store datadata = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,        'Age': [35, 70, 45, 20] }  # Creating DataFramedf = pd.DataFrame(data) # Creating an empty listres=[] # Iterating through the columns of# dataframefor 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 listprint(res)
 
 
Output: 
 
[['Tony', 'Steve', 'Bruce', 'Peter'], [35, 70, 45, 20]]
 
Method #3: Converting a DataFrame to a list that contains lists having all the columns of a row.
 
Python3
import pandas as pd  # Creating a dictionary to store datadata = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,        'Age': [35, 70, 45, 20] }  # Creating DataFramedf = pd.DataFrame(data)  # Converting dataframe to listli = df.values.tolist() # Printing listprint(li)
 
 
Output :
 
[['Tony', 35], ['Steve', 70], ['Bruce', 45], ['Peter', 20]]
 
Method #4: Converting a DataFrame to a list that contains lists having all the columns of a row along with column names.
 
Python3
import pandas as pd  # Creating a dictionary to store datadata = {'Name':['Tony', 'Steve', 'Bruce', 'Peter' ] ,        'Age': [35, 70, 45, 20] }  # Creating DataFramedf = pd.DataFrame(data)  # Converting dataframe to listli = [df.columns.values.tolist()] + df.values.tolist() # Printing listprint(li)
 
 
Output:
 
[[‘Name’, ‘Age’], [‘Tony’, 35], [‘Steve’, 70], [‘Bruce’, 45], [‘Peter’, 20]]
 
simmytarika5
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()