Unstacking certain columns only in Pandas DataFrame
Start your free 7-days trial now!
Pandas' unstack(~)
method does not allow you to select specific columns to unstack. To unstack certain columns only, use the Pandas melt(~)
method.
Example
Consider the following DataFrame:
import pandas as pddf
A B C0 a None 51 b c 52 a c 63 b None 6
Solution
To unstack columns A
and B
only and keep column C
:
Explanation
We first use the melt(~)
method to elongate (please refer to our documentation on melt(~)
to understand what this means) the DataFrame based on columns A
and B
:
C variable value0 5 A a1 5 A b2 6 A a3 6 A b4 5 B None5 5 B c6 6 B c7 6 B None
By default, melt(~)
creates a new column variable
that indicate the field that the value
column refers to (e.g. the first row tells us that the value for column A
is a
).
For the purpose of unstacking, we don't need this variable
column, so we can use the drop(~)
method to remove this column:
Finally, if we want to remove rows where value is None
, we can use the query(~)
method:
Here, the query('value == value')
fetches rows where the value for the value
column is not missing - this works because of the special property NaN != NaN
.