Here's the problem: I know how to merge two DataFrames on indexes or on columns, but I am not able to merge them on a both indexes and columns.
I have two DataFrames and I want to merge them on indexes (which are dates) and on column id. I created some sample data to better explain my problem.
from datetime import date
import numpy as np
import pandas as pd
np.random.seed(200)
dates = [date(2020, 1, 31), date(2020, 2, 28)]
a = {"id": ["A", "B"] * len(dates), "w": [.5, .5] * len(dates)}
b = {"id": ["B", "A"] * len(dates), "x": np.random.random(2 * len(dates))}
a = pd.DataFrame(a, index=dates * len(dates))
b = pd.DataFrame(b, index=dates * len(dates))
Desired output:
id w x
2020-01-31 A 0.5 0.226547
2020-02-28 B 0.5 0.947632
2020-01-31 A 0.5 0.428309
2020-02-28 B 0.5 0.594420
Please note that I am searching for a general solution, where a and b do not necessarily contains the same indexes or elements in id.