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

Question:

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
Asked By: RogerKint

||

Answers:

You could use stack after setting your index to be ‘Z’ with some basic manipulation like renaming and dropping:

# Setup
df = pd.DataFrame({"X" : [1, 2, 3], "Y" : [4, 5, 6], "Z": [True, True, False]})

# Reshape
df.set_index('Z').stack().reset_index().rename({0: 'X'},axis=1).sort_values('X')[['X','Z']]

prints:

# Output
   X      Z
0  1   True
2  2   True
4  3  False
1  4   True
3  5   True
5  6  False
Answered By: sophocles

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

Answered By: Mustafa Aydın

Another possible solution, based on pandas.concat:

pd.concat([df[['X','Z']], df[['Y','Z']].rename({'Y': 'X'}, axis=1)])

Output:

   X      Z
0  1   True
1  2   True
2  3  False
0  4   True
1  5   True
2  6  False
Answered By: PaulS

One option is to reshape with pivot_longer from pyjanitor, where names_to is a list of the new name(s), and names_pattern, is a list of the matching patterns:

# pip install pyjanitor
import pandas as pd
import janitor

df.pivot_longer(index='Z', names_to = ['X'], names_pattern = ['.'])
       Z  X
0   True  1
1   True  2
2  False  3
3   True  4
4   True  5
5  False  6
Answered By: sammywemmy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.