How to Create a List Consisting of Dataframe Row in Python

To create a list consisting of dataframe rows in Python, you can use the values attribute along with the tolist() function.

The following example shows how to create a list consisting of dataframe rows in Python using the values and tolist() functions.

Using values Attribute & tolist() Function

We can use the values attribute and the tolist() function to create a list from dataframe rows.

Suppose we have the following dataframe:

# Import pandas library
import pandas as pd

# Create the office_stuff dataframe
office_stuff = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Create list from dataframe rows
list_ = office_stuff.values.tolist()

# Show list
print(list_)

Output: 👇️

[['01-03-2023', 'A-101', 'Laptop', 4500, 1], ['01-03-2023', 'A-102', 'Mobile', 550, 1], ['01-03-2023', 'A-103', 'Printer', 250, 1], ['01-03-2023', 'B-101', 'Keyboard', 50, 0], ['02-03-2023', 'B-102', 'Scanner', 350, 1], ['02-03-2023', 'B-104', 'Mouse', 50, 1]]

In this example, we use the values attribute and the tolist() function to create a list list_ from the rows of the dataframe office_stuff.

The output shows the list created from the rows of the dataframe.

Conclusion

We can use the values attribute and the tolist() function to create a list consisting of dataframe rows in Python.