1

I want to merge df1 and df2. I used concat function with outer join and use multi index.

The result is merged index value,, I want to divide index columns..

Please advise me how to do.

   df1:
        CODE    U-01    U-02    U-03    U-04    U-05        
  INDEX host                                                                                    
   L1   A        3.0    3.0     3.0     3.0      3.0        

   L2   B        3.0    3.0     3.0     3.0      3.0

   L3   C        3.0    3.0     3.0     3.0      3.0    

   L4   D        3.0    3.0     3.0     3.0      3.0


  df2:
  CODE      U-01     U-02   U-03    U-04    U-05    
  LEVEL      H        L       H      M        L 
  STANDARD   3        3       3      3        2

so, My code is ,

 total_data = pd.concat([df1, df2], join='outer')

But, The result is

            U-01    U-02    U-03    U-04    U-05        
 LEVEL       H       L        H       M       L 
STANDARD     3       3        3       3       2                                                                     
 (L1,A)     3.0     3.0      3.0     3.0     3.0        

 (L2,B)     3.0     3.0      3.0     3.0     3.0

 (L3,C)     3.0     3.0      3.0     3.0     3.0    

 (L4,D)     3.0     3.0      3.0     3.0     3.0

I want to split the column and use multi index.

 Desired result would be :

             U-01   U-02    U-03    U-04    U-05        
 LEVEL        H       L       H       M       L 
 STANDARD     3       3       3       3       2                                                                     
 L1      A   3.0      3.0    3.0     3.0     3.0        

 L2      B   3.0      3.0    3.0     3.0     3.0

 L3      C   3.0      3.0    3.0     3.0     3.0    

 L4      D   3.0      3.0    3.0     3.0     3.0
  

index = INDEX, host
2
  • 1
    What is print (df1.index.tolist()), print (df1.columns.tolist()), print (df2.index.tolist()), print (df2.columns.tolist()) ? Commented Aug 20, 2020 at 6:29
  • df1.index : MultiIndex[(L1,A], (L2,B), (L3,C), (L4,D)] df1. columns : Index['U-01', 'U-02', 'U-03', 'U-04', 'U-05'] df2.index : Index['LEVEL', 'STANDARD'] df2. columns : Index['U-01', 'U-02', 'U-03', 'U-04', 'U-05'] Commented Aug 20, 2020 at 6:35

1 Answer 1

1

You need MultiIndex in index in both DataFrames before concat.

So use:

df2 = df2.assign(new = '').set_index('new', append=True)

total_data = pd.concat([df1, df2])

Then second level is filled by spaces in second level (what is a bit trick, because cannot see it).

Also is possible add some value to second level (now it is possible see, there is value new):

df2 = df2.assign(new = 'new').set_index('new', append=True)

total_data = pd.concat([df1, df2])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.