Skip to content Skip to sidebar Skip to footer

Join Dataframes - One With Multiindex Columns And The Other Without

I'm trying to join two dataframes - one with multiindex columns and the other with a single column name. They have similar index. I get the following warning: 'UserWarning: merging

Solution 1:

It depends on what you want! Do you want the column from df2 to be aligned with the 1st or second level of columns from df?

You have to add a level to the columns of df2

Super cheezy with pd.concat

df.join(pd.concat([df2], axis=1, keys=['a']))

Better way

df2.columns = pd.MultiIndex.from_product([['a'], df2.columns])

df.join(df2)

enter image description here

Solution 2:

I think simpliest is create MultiIndex in df2 and then use concat or join:

df2.columns = pd.MultiIndex.from_tuples([('a','w')])
print (df2)
          a
          w
A -0.562729
B -0.212032
C  0.102451
df2.columns = [['a'], df2.columns]
print (df2)
          a
          w
A -1.253881
B -0.637752
C  0.907105

df3 = pd.concat([df, df2], axis=1)

Or:

df3 = df.join(df2)

print (df3)
first        bar                 baz                 foo                 qux  \
secondone       two       one       two       one       two       one   
A      -0.2696670.2215661.1383930.871762-0.063132-1.995682-0.797885   
B      -0.4568780.293350-1.040748-1.3078710.0024621.580711-0.198943   
C      -0.691755-0.279445-0.809215-0.0066581.4524840.516414-0.295961first                    a  
second       two         w  
A       1.068843-0.562729  
B       1.247057-0.212032  
C      -0.3453000.102451

Post a Comment for "Join Dataframes - One With Multiindex Columns And The Other Without"