Categories
Python Answers

How to convert a Python Pandas DataFrame to a list of lists?

Spread the love

Sometimes, we want to convert a Python Pandas DataFrame to a list of lists.

In this article, we’ll look at how to convert a Python Pandas DataFrame to a list of lists.

How to convert a Python Pandas DataFrame to a list of lists?

To convert a Python Pandas DataFrame to a list of lists, we can use the tolist method.

For instance, we write

df = pd.DataFrame([[1,2,3],[3,4,5]])
lol = df.values.tolist()

to create a data frame df.

Then we convert the df data frame a to a list of lists with

df.values.tolist()

and assigned the returned nested list to lol

Conclusion

To convert a Python Pandas DataFrame to a list of lists, we can use the tolist method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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