I have two DataFrames, each with a MultiIndex. The index columns have different names in the two DataFrames (in this minimal example, there is only a single index column in each of the DataFrames, but in a real application there could be multiple):
>>> print(flavor)
Price
Flavor
Vanilla 2.0
Chocolate 3.0
>>> print(topping)
Price
Topping
Syrup 0.4
Sprinkles 0.5
I want to combine the DataFrames by adding the "price" columns for all combinations. Expected result (the order of the rows or index columns is not important):
Price
Flavor Topping
Vanilla Syrup 2.4
Sprinkles 2.5
Chocolate Syrup 3.4
Sprinkles 3.5
This seems like a fairly straightforward operation. But apparently, I'm missing something, because none of these work:
combined = flavor + topping
combined = flavor.add(topping)
combined = flavor.combine(topping, lambda a, b: a+b)
Instead, I get an exception:
ValueError: cannot join with no overlapping index names
Full example:
import pandas as pd
flavor_index = pd.MultiIndex.from_tuples([("Vanilla",), ("Chocolate",)], names=["Flavor"])
flavor = pd.DataFrame({"Price": [2.0, 3.0]}, index=flavor_index)
print(flavor)
topping_index = pd.MultiIndex.from_tuples([("Syrup",), ("Sprinkles",)], names=["Topping"])
topping = pd.DataFrame({"Price": [0.4, 0.5]}, index=topping_index)
print(topping)
# ValueError: cannot join with no overlapping index names
# combined = flavor + topping
# combined = flavor.add(topping)
combined = flavor.combine(topping, lambda a, b: a+b)
print(combined)