如何用Pandas将DataFrame d1转为d2结构?能否用pivot函数?
Hey there! Let's walk through this Pandas reshaping question step by step.
First, let's recap your data structures to make sure we're on the same page:
Original wide-format DataFrame
d1:where p1 p2 0 x 3 11 1 y 7 12Target long-format DataFrame
d2:where who value 0 x p1 3 1 x p2 11 2 y p1 7 3 y p2 12
Why pivot() doesn't work for this conversion
First off, let's clear up a common point of confusion: pivot() is designed to convert long-format data to wide-format data—it's the reverse of what you're trying to do here. So you can't use pivot() directly to turn d1 (wide) into d2 (long); that's not what it's built for.
The correct way: Use melt() for wide-to-long conversion
You mentioned you already found this working solution, but let's formalize it for clarity:
import pandas as pd # Define original d1 d1 = pd.DataFrame({'where':['x','y'], 'p1':[3,7], 'p2':[11,12]}) # Reshape to long format (d2) d2 = d1.melt( id_vars='where', # Keep this column as the identifier var_name='who', # Rename the original column names to 'who' value_name='value'# Rename the values column to 'value' )
This will produce exactly the d2 structure you're targeting.
Converting back from long to wide (d2 to d1)
Now, if you wanted to convert d2 back to the original d1 structure, that's where pivot() shines! Here's how to do it:
# Use pivot to reshape back to wide format d1_again = d2.pivot( index='where', # Use 'where' as the row index columns='who', # Use 'who' values as column names values='value' # Populate columns with 'value' data ).reset_index() # Clean up the column name hierarchy d1_again.columns.name = None
If you're working with data that might have duplicate entries for the same where/who pair, pivot_table() is a safer choice (just specify an aggregation function like 'first' or 'sum'):
d1_again = d2.pivot_table( index='where', columns='who', values='value', aggfunc='first' ).reset_index() d1_again.columns.name = None
内容的提问来源于stack exchange,提问作者baxx

