[Python] Pandas Dataframe Multiindex Merge

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    Here is a hypothetical scenario with multiindex dataframes in pandas. Trying to merge them will result in an error. Do I have to do reset_index() on either dataframe to make this work?

    arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
    ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
    tuples = list(zip(*arrays))
    index1 = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
    index2 = pd.MultiIndex.from_tuples(tuples, names=['third', 'fourth'])

    s1 = pd.DataFrame(np.random.randn(8), index=index1, columns=['s1'])
    s2 = pd.DataFrame(np.random.randn(8), index=index2, columns=['s2'])


    Attempted merges:

    s1.merge(s2, how='left', left_index=True, right_index=True)

    • Editor's note: The error for this one was most likely ValueError: cannot join with no overlapping index names. Tested with Pandas 2.2.3

    s1.merge(s2, how='left', left_on=['first', 'second'], right_on=['third', 'fourth'])

    • Editor's note: It's not clear what error occurred here. If you know, please add it.

    Continue reading...

Compartilhe esta Página