Pandas merge woes on MultiIndex, solved

Perhaps you are data munging in Python, using “pandas”, and you attempt to use DataFrame.merge() in order to put two DataFrames together.

“cannot join with no level specified and no overlapping names”

This happens when you have two DataFrames with one having a MultiIndex type, which *could* play nice together (e.g. you have “year, month” on the left, and “year” on the right, *but do not have names set.*

You’ll need to explicitly set names with

leftdf.index.levels[0].name = “onename”
leftdf.index.levels[1].name = “twoname”
rightdf.index.levels[0].name = “onename”

Alternatively, you can make it work if you reindex the right-hand side by the left hand side:

rightdf2 = rightdf.reindex(index=leftdf.index, level=0) ## NOTE Assignment, does not modify rightdf in-place

5 Responses to “Pandas merge woes on MultiIndex, solved”

  1. chuyelchulo says:

    Thanks, that resolved that very cryptic error message.

  2. Meng Lee says:

    Thanks! It helped me a lot 🙂

  3. Mark says:

    Thanks for this. It worked for me.

  4. Gustavo Coelho says:

    Life saver, thank you!

  5. Davi Bicudo says:

    Great, thanks a lot!!!

Leave a Reply