Pandas Dataframe Multiindex Merge

前端 未结 4 429
萌比男神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:23

    Other than using the indexes names as pointed by @ALollz, you can simply use loc, which will match indexes automatically

    s1.loc[:, 's2'] = s2   # Or explicitly, s2['s2']
    
                    s1           s2
    first   second      
    bar     one     -0.111384   -2.341803
            two     -1.226569    1.308240
    baz     one      1.880835    0.697946
            two     -0.008979   -0.247896
    foo     one      0.103864   -1.039990
            two      0.836931    0.000811
    qux     one     -0.859005   -1.199615
            two     -0.321341   -1.098691
    

    A general formula would be

    s1.loc[:, s2.columns] = s2
    

提交回复
热议问题