I have a pandas dataframe with two id variables:
df = pd.DataFrame({'id': [1,1,1,2,2,3],                 'num': [10,10,12,13,14,15],                'q': ['a', 'b', 'd', 'a', 'b', 'z'],                'v': [2,4,6,8,10,12]})     id  num  q   v 0   1   10  a   2 1   1   10  b   4 2   1   12  d   6 3   2   13  a   8 4   2   14  b  10 5   3   15  z  12   I can pivot the table with:
df.pivot('id','q','v')   And end up with something close:
q    a   b   d   z id                 1    2   4   6 NaN 2    8  10 NaN NaN 3  NaN NaN NaN  12   However, what I really want is (the original unmelted form):
id   num   a   b   d   z                1    10   2   4 NaN NaN 1    12 NaN NaN   6 NaN   2    13   8 NaN NaN NaN 2    14 NaN  10 NaN NaN 3    15 NaN NaN NaN  12   In other words:
Update
I found a close solution from Wes McKinney's blog:
df.pivot_table(index=['id','num'], columns='q')           v             q        a   b   d   z id num                 1  10    2   4 NaN NaN    12  NaN NaN   6 NaN 2  13    8 NaN NaN NaN    14  NaN  10 NaN NaN 3  15  NaN NaN NaN  12   However, the format is not quite the same as what I want above.
We can use pivot() function to unmelt a DataFrame object and get the original dataframe. The pivot() function 'index' parameter value should be same as the 'id_vars' value. The 'columns' value should be passed as the name of the 'variable' column. The unmelted DataFrame values are the same as the original DataFrame.
In pandas, you can use the melt() function to unpivot a DataFrame – converting it from a wide format to a long format. This function uses the following basic syntax: df_unpivot = pd. melt(df, id_vars='col1', value_vars=['col2', 'col3', ...])
You can use the following basic syntax to convert a pandas DataFrame from a wide format to a long format: df = pd. melt(df, id_vars='col1', value_vars=['col2', 'col3', ...]) In this scenario, col1 is the column we use as an identifier and col2, col3, etc.
You could use set_index and unstack
In [18]: df.set_index(['id', 'num', 'q'])['v'].unstack().reset_index() Out[18]: q  id  num    a     b    d     z 0   1   10  2.0   4.0  NaN   NaN 1   1   12  NaN   NaN  6.0   NaN 2   2   13  8.0   NaN  NaN   NaN 3   2   14  NaN  10.0  NaN   NaN 4   3   15  NaN   NaN  NaN  12.0 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With