DeAnna
DeAnna

Reputation: 402

Need to stack specific columns and leave the others unchanged

I want to stack column B and C and leave the other columns as is.

This is what I have

    A      B     C   D
0  a1  hello1   bye1  d1
1  a2  hello2   bye2  d2
2  a3  hello3   bye3  d3
3  a4  hello4   bye4  d4

I've tried using the melt and stack functions. Can't get what I want.

This is what I want

    A      B     C   D
0  a1  hello1        d1
1  a2  hello2        d2
2  a3  hello3        d3
3  a4  hello4        d4
4       bye1
5       bye2
6       bye3
7       bye4

Upvotes: 0

Views: 46

Answers (3)

tawab_shakeel
tawab_shakeel

Reputation: 3739

use pd.concat()

df= pd.DataFrame(data={"A":['a1','a2','a3','a4'],
                      "B":["hello1","hello2","hello3","hello4"],
                      "C":["bye1","bye2","bye3","bye4"],
                      "D":["d1","d2","d3","d4"]})

df = pd.concat([df,pd.DataFrame(df['C'].values,columns=['B'])],sort=False,ignore_index=True)
df['C'] = ''
df.fillna('',inplace=True)
     A    B     C    D
0   a1  hello1      d1
1   a2  hello2      d2
2   a3  hello3      d3
3   a4  hello4      d4
4       bye1        
5       bye2        
6       bye3        
7       bye4        

in addition to your question

res = pd.DataFrame({'B': df['C'].values,'A': df['D'].values})
final_res = pd.concat([df,res],sort=False,ignore_index=True)
final_res[['C','D']]=''
print(final_res)
A       B      C  D
0  a1  hello1    
1  a2  hello2    
2  a3  hello3    
3  a4  hello4    
4  d1    bye1    
5  d2    bye2    
6  d3    bye3    
7  d4    bye4  

Upvotes: 1

anky
anky

Reputation: 75080

Trying another way:

cols=['B','C']
final=(df.reindex(range(df.shape[0]*len(cols)))
         .assign(B=df.melt(df.columns^cols).value,C=np.nan).fillna(''))

    A       B  C   D
0  a1  hello1     d1
1  a2  hello2     d2
2  a3  hello3     d3
3  a4  hello4     d4
4        bye1      
5        bye2      
6        bye3      
7        bye4      

Upvotes: 1

Anthony R
Anthony R

Reputation: 2939

Here's a possible solution (although, there may be a more efficient method)

  • Setup initial DF

df = pd.DataFrame([["a1", "Hello1","bye1", "d1"], ["a2", "Hello2","bye2", "d2"], ["a3", "Hello3","bye3", "d3"]], columns=["A", "B", "C", "D"])

  • Make copy of original DF

df2 = df.copy()

  • Reorder/Remove all columns except desired column

df2["A"] = ""

df2["B"] = df2["C"]

df2["C"] = ""

df2["D"] = ""

  • Concatenate original DF with the altered DF.

result = pd.concat([df, df2])

result["C"] = "" - remove the entries in col "C".

results df

Upvotes: 1

Related Questions