Pandas Dataframe Multiindex Merge

前端 未结 4 431
萌比男神i
萌比男神i 2020-12-16 14:31

I wanted to ask a questions regarding merging multiindex dataframe in pandas, here is a hypothetical scenario:

arrays = [[\'bar\', \'bar\', \'baz\', \'baz\'         


        
4条回答
  •  星月不相逢
    2020-12-16 15:01

    rename_axis

    You can rename the index levels of one and let join do its thing

    s1.join(s2.rename_axis(s1.index.names))
    
                        s1        s2
    first second                    
    bar   one    -0.696420 -1.040463
          two     0.640891  1.483262
    baz   one     1.598837  0.097424
          two     0.003994 -0.948419
    foo   one    -0.717401  1.190019
          two    -1.201237 -0.000738
    qux   one     0.559684 -0.505640
          two     1.979700  0.186013
    

    concat

    pd.concat([s1, s2], axis=1)
    
                        s1        s2
    first second                    
    bar   one    -0.696420 -1.040463
          two     0.640891  1.483262
    baz   one     1.598837  0.097424
          two     0.003994 -0.948419
    foo   one    -0.717401  1.190019
          two    -1.201237 -0.000738
    qux   one     0.559684 -0.505640
          two     1.979700  0.186013
    

提交回复
热议问题