Pandas : Convert a DataFrame into a list of rows or columns in python | (list of lists)

In this article, we will discuss how to convert a dataframe into a list of lists, by converting either each row or column into a list and create a python list of lists from them.

First of all, create a dataframe,

import pandas as pd

# List of Tuples
students = [('jack', 34, 'Sydney', 155),
            ('Riti', 31, 'Delhi', 177.5),
            ('Aadi', 16, 'Mumbai', 81),
            ('Mohit', 31, 'Delhi', 167),
            ('Veena', 12, 'Delhi', 144),
            ('Shaunak', 35, 'Mumbai', 135),
            ('Shaun', 35, 'Colombo', 111)
            ]

# Create a DataFrame object
studentDfObj = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score'])

print(studentDfObj)

Contents of the dataframe object studentDfObj are,

      Name  Age     City  Score
0     jack   34   Sydney  155.0
1     Riti   31    Delhi  177.5
2     Aadi   16   Mumbai   81.0
3    Mohit   31    Delhi  167.0
4    Veena   12    Delhi  144.0
5  Shaunak   35   Mumbai  135.0
6    Shaun   35  Colombo  111.0

Convert a Dataframe into a list of lists – Rows Wise

In the above-created dataframe, we have to fetch each row as a list and create a list of these lists. Let’s do all that in a single line,

# Convert a dataframe to the list of rows i.e. list of lists
listOfDFRows = studentDfObj.to_numpy().tolist()

print(listOfDFRows)
print(type(listOfDFRows))

Output:

[['jack', 34, 'Sydney', 155.0], ['Riti', 31, 'Delhi', 177.5], ['Aadi', 16, 'Mumbai', 81.0], ['Mohit', 31, 'Delhi', 167.0], ['Veena', 12, 'Delhi',
144.0], ['Shaunak', 35, 'Mumbai', 135.0], ['Shaun', 35, 'Colombo', 111.0]]
<class 'list'>

It converted the dataframe into a list of lists row-wise, i.e., each nested list contains a row of the dataframe. But what happened in a single line?

How did it work?

Let’s break the above single line into multiple lines to understand the concept behind it.

Step 1: Convert the Dataframe to a nested Numpy array using DataFrame.to_numpy() i.e.,

# get rows of a dataframe as a nested numpy array
numpy_2d_array = studentDfObj.to_numpy()

print(numpy_2d_array)
print(type(numpy_2d_array))

Output:

[['jack' 34 'Sydney' 155.0]
 ['Riti' 31 'Delhi' 177.5]
 ['Aadi' 16 'Mumbai' 81.0]
 ['Mohit' 31 'Delhi' 167.0]
 ['Veena' 12 'Delhi' 144.0]
 ['Shaunak' 35 'Mumbai' 135.0]
 ['Shaun' 35 'Colombo' 111.0]]
<class 'numpy.ndarray'>

DataFrame.to_numpy()  converts a dataframe to a Numpy array. Therefore we got a 2D Numpy array here. We confirmed that by printing the type of the returned object.

Step 2: Convert 2D Numpy array into a list of lists

Numpy provides a function tolist(), which converts a Numpy Array into a list. Let’s call that function to the above created 2D Numpy array object,

# Convert 2D numpy array to the list of lists
listOfDFRows = numpy_2d_array.tolist()

print(listOfDFRows)
print(type(listOfDFRows))

Output:

[['jack', 34, 'Sydney', 155.0], ['Riti', 31, 'Delhi', 177.5], ['Aadi', 16, 'Mumbai', 81.0], ['Mohit', 31, 'Delhi', 167.0], ['Veena', 12, 'Delhi',
144.0], ['Shaunak', 35, 'Mumbai', 135.0], ['Shaun', 35, 'Colombo', 111.0]]
<class 'list'>

It turned the 2D Numpy Array into a list of lists.

So, this is how we transformed a dataframe into a 2D Numpy Array and then into a List of Lists, where each nested list represents a row of the dataframe.

Convert a Dataframe into a list of lists – Column Wise

Contents of the dataframe studentDfObj are,

      Name  Age     City  Score
0     jack   34   Sydney  155.0
1     Riti   31    Delhi  177.5
2     Aadi   16   Mumbai   81.0
3    Mohit   31    Delhi  167.0
4    Veena   12    Delhi  144.0
5  Shaunak   35   Mumbai  135.0
6    Shaun   35  Colombo  111.0

Now to convert each column into a list and create a list of these lists,

# Convert a dataframe to the list of columns i.e. list of lists
listOfDFRows = studentDfObj.transpose().values.tolist()

print(listOfDFRows)
print(type(listOfDFRows))

Output:

[['jack', 'Riti', 'Aadi', 'Mohit', 'Veena', 'Shaunak', 'Shaun'], [34, 31, 16, 31, 12, 35, 35], ['Sydney', 'Delhi', 'Mumbai', 'Delhi', 'Delhi', 'Mu
mbai', 'Colombo'], [155.0, 177.5, 81.0, 167.0, 144.0, 135.0, 111.0]]
<class 'list'>

How did it work?

It worked on the same concept we discussed above, just one additional step here i.e.

Step 1: Transpose the dataframe to convert rows as columns and columns as rows

# Transpose the dataframe, rows are now columns and columns are now rows
transposedDfObj = studentDfObj.transpose()

print(transposedDfObj)

Output

            0      1       2      3      4        5        6
Name     jack   Riti    Aadi  Mohit  Veena  Shaunak    Shaun
Age        34     31      16     31     12       35       35
City   Sydney  Delhi  Mumbai  Delhi  Delhi   Mumbai  Colombo
Score     155  177.5      81    167    144      135      111

tansposedDFObj is a a transpose of the original dataframe i.e. rows in studentDfObj are columns in  tansposedDFObj and columns in studentDfObj are rows in tansposedDFObj.

Step 2: Convert the Dataframe to a nested Numpy array using DataFrame.to_numpy()

# get rows of a dataframe as a nested numpy array
numpy_2d_array = transposedDfObj.to_numpy()

print(numpy_2d_array)
print(type(numpy_2d_array))

Output

[['jack' 'Riti' 'Aadi' 'Mohit' 'Veena' 'Shaunak' 'Shaun']
 [34 31 16 31 12 35 35]
 ['Sydney' 'Delhi' 'Mumbai' 'Delhi' 'Delhi' 'Mumbai' 'Colombo']
 [155.0 177.5 81.0 167.0 144.0 135.0 111.0]]
<class 'numpy.ndarray'>

Step 3: Convert 2D Numpy array into a list of lists.

# Convert 2D numpy array to the list of lists
listOfDFRows = numpy_2d_array.tolist()

print(listOfDFRows)
print(type(listOfDFRows))

Output

[['jack', 'Riti', 'Aadi', 'Mohit', 'Veena', 'Shaunak', 'Shaun'], [34, 31, 16, 31, 12, 35, 35], ['Sydney', 'Delhi', 'Mumbai', 'Delhi', 'Delhi', 'Mu
mbai', 'Colombo'], [155.0, 177.5, 81.0, 167.0, 144.0, 135.0, 111.0]]
<class 'list'>

It converted the 2D Numpy Array into a list of lists. So, this is how we transformed a dataframe into a 2D Numpy Array and then into a List of Lists, where each nested list represents a column of the dataframe.

The Complete example is as follows,

import pandas as pd

def main():
   # List of Tuples
   students = [('jack', 34, 'Sydney', 155),
               ('Riti', 31, 'Delhi', 177.5),
               ('Aadi', 16, 'Mumbai', 81),
               ('Mohit', 31, 'Delhi', 167),
               ('Veena', 12, 'Delhi', 144),
               ('Shaunak', 35, 'Mumbai', 135),
               ('Shaun', 35, 'Colombo', 111)
               ]

   # Create a DataFrame object
   studentDfObj = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Score'])

   print("Contents of the Dataframe : ")
   print(studentDfObj)

   print('Convert a Dataframe into list of lists - Rows Wise')

   # Convert a dataframe to the list of rows i.e. list of lists
   listOfDFRows = studentDfObj.to_numpy().tolist()

   print(listOfDFRows)
   print(type(listOfDFRows))

   print('How did it worked ?')

   # get rows of a dataframe as a nested numpy array
   numpy_2d_array = studentDfObj.to_numpy()

   print(numpy_2d_array)
   print(type(numpy_2d_array))

   # Convert 2D numpy array to the list of lists
   listOfDFRows = numpy_2d_array.tolist()

   print(listOfDFRows)
   print(type(listOfDFRows))

   print('Convert Dataframe into lists of lists - Column Wise')

   # Convert a dataframe to the list of columns i.e. list of lists
   listOfDFRows = studentDfObj.transpose().values.tolist()

   print(listOfDFRows)
   print(type(listOfDFRows))

   print('How did it worked ?')

   # Transpose the dataframe, rows are now columns and columns are now rows
   transposedDfObj = studentDfObj.transpose()

   print(transposedDfObj)

   # get rows of a dataframe as a nested numpy array
   numpy_2d_array = transposedDfObj.to_numpy()

   print(numpy_2d_array)
   print(type(numpy_2d_array))

   # Convert 2D numpy array to the list of lists
   listOfDFRows = numpy_2d_array.tolist()

   print(listOfDFRows)
   print(type(listOfDFRows))


if __name__ == '__main__':
   main()

Output:

Contents of the Dataframe :
      Name  Age     City  Score
0     jack   34   Sydney  155.0
1     Riti   31    Delhi  177.5
2     Aadi   16   Mumbai   81.0
3    Mohit   31    Delhi  167.0
4    Veena   12    Delhi  144.0
5  Shaunak   35   Mumbai  135.0
6    Shaun   35  Colombo  111.0
Convert a Dataframe into list of lists - Rows Wise
[['jack', 34, 'Sydney', 155.0], ['Riti', 31, 'Delhi', 177.5], ['Aadi', 16, 'Mumbai', 81.0], ['Mohit', 31, 'Delhi', 167.0], ['Veena', 12, 'Delhi',
144.0], ['Shaunak', 35, 'Mumbai', 135.0], ['Shaun', 35, 'Colombo', 111.0]]
<class 'list'>
How did it worked ?
[['jack' 34 'Sydney' 155.0]
 ['Riti' 31 'Delhi' 177.5]
 ['Aadi' 16 'Mumbai' 81.0]
 ['Mohit' 31 'Delhi' 167.0]
 ['Veena' 12 'Delhi' 144.0]
 ['Shaunak' 35 'Mumbai' 135.0]
 ['Shaun' 35 'Colombo' 111.0]]
<class 'numpy.ndarray'>
[['jack', 34, 'Sydney', 155.0], ['Riti', 31, 'Delhi', 177.5], ['Aadi', 16, 'Mumbai', 81.0], ['Mohit', 31, 'Delhi', 167.0], ['Veena', 12, 'Delhi',
144.0], ['Shaunak', 35, 'Mumbai', 135.0], ['Shaun', 35, 'Colombo', 111.0]]
<class 'list'>
Convert Dataframe into lists of lists - Column Wise
[['jack', 'Riti', 'Aadi', 'Mohit', 'Veena', 'Shaunak', 'Shaun'], [34, 31, 16, 31, 12, 35, 35], ['Sydney', 'Delhi', 'Mumbai', 'Delhi', 'Delhi', 'Mu
mbai', 'Colombo'], [155.0, 177.5, 81.0, 167.0, 144.0, 135.0, 111.0]]
<class 'list'>
How did it worked ?
            0      1       2      3      4        5        6
Name     jack   Riti    Aadi  Mohit  Veena  Shaunak    Shaun
Age        34     31      16     31     12       35       35
City   Sydney  Delhi  Mumbai  Delhi  Delhi   Mumbai  Colombo
Score     155  177.5      81    167    144      135      111
[['jack' 'Riti' 'Aadi' 'Mohit' 'Veena' 'Shaunak' 'Shaun']
 [34 31 16 31 12 35 35]
 ['Sydney' 'Delhi' 'Mumbai' 'Delhi' 'Delhi' 'Mumbai' 'Colombo']
 [155.0 177.5 81.0 167.0 144.0 135.0 111.0]]
<class 'numpy.ndarray'>
[['jack', 'Riti', 'Aadi', 'Mohit', 'Veena', 'Shaunak', 'Shaun'], [34, 31, 16, 31, 12, 35, 35], ['Sydney', 'Delhi', 'Mumbai', 'Delhi', 'Delhi', 'Mu
mbai', 'Colombo'], [155.0, 177.5, 81.0, 167.0, 144.0, 135.0, 111.0]]
<class 'list'>

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top