Reversing the Transformation: Converting a Pandas DataFrame to a List of Lists
It is straightforward to create a pandas DataFrame from a list of lists. However, converting a DataFrame back to a list of lists requires a different approach. This question examines the methods for extracting the data from a DataFrame and converting it into the desired format.
To achieve the conversion, the following steps can be taken:
Retrieving the Underlying Array:
Converting to a List:
For example:
<code class="python">import pandas as pd # Create a DataFrame df = pd.DataFrame([[1, 2, 3], [3, 4, 5]]) # Extract the underlying NumPy array array = df.values # Convert to a list of lists list_of_lists = array.tolist() # Print the result print(list_of_lists) # Output: [[1, 2, 3], [3, 4, 5]]</code>
By following these steps, you can effectively convert a pandas DataFrame back to a list of lists, retaining the original data.
The above is the detailed content of How do I convert a Pandas DataFrame back to a list of lists?. For more information, please follow other related articles on the PHP Chinese website!