Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to concat column Y to column X and replicate values Z in pandas dataframe?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading