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>
Output:
[[1, 2, 3], [3, 4, 5]]
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!