How to Convert a Pandas DataFrame Back into a List of Lists?

DDD
Release: 2024-11-02 15:09:30
Original
936 people have browsed it

How to Convert a Pandas DataFrame Back into a List of Lists?

Reversing the Conversion: Pandas DataFrame to List of Lists

Creating a list of lists from a pandas DataFrame is a simple operation. However, the reverse process, converting a DataFrame back into a list of lists, may not be immediately obvious.

Problem Statement:

How do we convert a pandas DataFrame back into a list of lists?

Solution:

To transform a DataFrame into a list of lists, we can utilize the underlying array and its tolist() method. Here's an example:

<code class="python">import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame([[1,2,3],[3,4,5]])

# Convert the DataFrame to a list of lists
list_of_lists = df.values.tolist()

# Print the resulting list of lists
print(list_of_lists)</code>
Copy after login

Output:

[[1, 2, 3], [3, 4, 5]]
Copy after login

The above is the detailed content of How to Convert a Pandas DataFrame Back into a List of Lists?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template