How do convert DataFrame to lists in Python

In this article, we will learn How do convert DataFrame to lists in Python with different examples. The pandas dataframe use to store data into tabular form,dataframe can be created by loading the dataset from Excel, SQL database, CSV as well as from List, Dictionary, List of the dictionary, ere we are Python provides a built-in method df.values.tolist() to accomplish We will learn multiple ways.

1. tolist() to Convert DataFrame to list of lists


These are the step that includes how to create and convert a DataFrame to a list of lists

  • Firstly need to import the pandas module using this “import pandas as pd
  • We have student data, that we will use to create a data frame Using pd.DataFrame() method.
  • Now we have a dataframe next step is to convert it into a List using df. values.tolist() method.
  • Finally, printing the converted dataframe into list using print() method and the output will be a list of lists.

Program Example

import pandas as pd

students = {'Subjects': ['Math','Pysic','chem','Eng'],
            'marks': [100,80,100,99]
            }

#creating a dataframe
df = pd.DataFrame(students, columns= ['Subjects', 'marks'])

#converting dataframe to list

Df_to_list = df.values.tolist()
print (Df_to_list)

Output will be a list of lists from dataframe

[['Math', 100], ['Pysic', 80], ['chem', 100], ['Eng', 99]]

2. Convert a DataFrame to List with all rows and columns name


We need to convert a dataframe into a list containing all rows and columns along with column names.

By using DataFrame. columns attribute can be done that is used to find the column labels of the DataFrame.

In our example, we will display a List with all rows and columns names.

Program example

import pandas as pd

students = {'Subjects': ['Math','Pysic','chem','Eng'],
            'marks': [100,80,100,99]
            }


#creating a dataframe
df = pd.DataFrame(students, columns= ['Subjects', 'marks'])

#converting dataframe to list
Df_to_list = [df.columns.values.tolist()] + df.values.tolist()
print (Df_to_list)

The output will be a list of lists with all rows and columns along with column names.

[['Subjects', 'marks'], ['Math', 100], ['Pysic', 80], ['chem', 100], ['Eng', 99]]

3. DataFrame to List with all rows of specific columns


We can convert an individual column of dataframe to a list bypassing the column name.

In this example, We are passing the column name “subject” to convert into the list.

Program Example

import pandas as pd

students = {'Subjects': ['Math','Pysic','chem','Eng'],
            'marks': [100,80,100,99]
            }

df = pd.DataFrame(students, columns= ['Subjects', 'marks'])

Df_to_list = df['Subjects'].tolist()
print (Df_to_list)

The output will be a list of all rows of column names ‘Subjects’.

['Math', 'Pysic', 'chem', 'Eng']

4. Data frame to a nested List with all rows and all columns


In this example, we are looping through all columns of the dataframe and one by one converting into a List and appending it to a new result list (result_list).

import pandas as pd

students = {'Subjects': ['Math','Pysic','chem','Eng'],
            'marks': [100,80,100,99]
            }


result_list = []

datafram = pd.DataFrame(students, columns= ['Subjects', 'marks'])
for column in datafram.columns:
    df_to_list = datafram[column].tolist()
    
    result_list.append(df_to_list)

print (result_list)

Output

[['Math', 'Pysic', 'chem', 'Eng'], [100, 80, 100, 99]]

5. Append columns while convert dataframe to list


At times we have need to append the new column into the existing dataframe, it can be done using the append() method. In this example, we can understand how to accomplish this for columns ‘Subjects’, ‘marks‘.

  • First get the column name of the dataframe like ‘Subjects’, ‘marks‘.
  • Use the append() method to append new values for all given column.
import pandas as pd

students = {'Subjects': ['Math','Pysic','chem','Eng'],
            'marks': [100,80,100,99]
            }



datafram = pd.DataFrame(students, columns= ['Subjects', 'marks'])
subjects = datafram['Subjects'].values.tolist()
marks = datafram['marks'].values.tolist()
subjects.append('Music')
marks.append(100)

print(subjects)
print(marks)

The output will be as a list for columns of ‘subject’ and ‘marks’ with appended items convert into a list individually.

['Math', 'Pysic', 'chem', 'Eng', 'Music']
[100, 80, 100, 99, 100]

Summary :

We have explored how to convert DataFrame into lists in Python with code examples in multiple ways that include tolist() method, DataFrame to List with all rows and columns name, Convert a DataFrame to List with all rows and columns name and ore.