I have a pandas DataFrame with three columns:
X Y Z
0 1 4 True
1 2 5 True
2 3 6 False
How do I make it so that I have two columns X and Z with values:
X Z
0 1 True
1 2 True
2 3 False
3 4 True
4 5 True
5 6 False
>Solution :
you can melt:
In [41]: df.melt(id_vars="Z", value_vars=["X", "Y"], value_name="XY")[["XY", "Z"]]
Out[41]:
XY Z
0 1 True
1 2 True
2 3 False
3 4 True
4 5 True
5 6 False
- identifier variable is "Z": it will be repeated as necessary against value variables…
- …which are X and Y
- name X and Y’s together column to "XY", and select that and "Z" at the end
(you can chain .rename(columns={"XY": "X"}) if you want that column to be named X again.)