0

I have 2 data frames with one common column denoting the row number.

Df1: 
                     
Rownum  A     B     C

11      S     V     L
11      F     U     M  
11      T     C     O  
11      B     X     P

Df2:
                      
Rownum  E     F     G
12      S     V     L
12      F     U     M  
12      T     C     O  
12      B     X     P

Current implementation:

df = pd.concat(df1,df2,axis=1)

Output:

Rownum  A     B     C   Rownum  E     F     G

11      S     V     L   12      S     V     L
11      F     U     M   12      F     U     M 
11      T     C     O   12      T     C     O 
11      B     X     P   12      B     X     P

Below mentioned is the desired output I'm trying to achieve:

Rownum  A     B     C    E     F     G

11      S     V     L
11      F     U     M  
11      T     C     O  
11      B     X     P
12                       S     V     L
12                       F     U     M  
12                       T     C     O  
12                       B     X     P

Any direction around this would be much appreciated.

Raj
  • 25
  • 5

1 Answers1

0

Remove axis=1 in concat with convert Rownum to index for both DataFrames:

df = pd.concat([df1.set_index('Rownum'),df2.set_index('Rownum')]).reset_index().fillna('')
print (df)
   Rownum  A  B  C  E  F  G
0      11  S  V  L         
1      11  F  U  M         
2      11  T  C  O         
3      11  B  X  P         
4      12           S  V  L
5      12           F  U  M
6      12           T  C  O
7      12           B  X  P
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252