lefth = pd.DataFrame({"key1": ["Ohio", "Ohio", "Ohio",
"Nevada", "Nevada"], # 左侧 DataFrame
"key2": [2000, 2001, 2002, 2001, 2002],
"data": pd.Series(range(5), dtype="Int64")})
righth_index = pd.MultiIndex.from_arrays([ # 右侧 DataFrame 的 MultiIndex
["Nevada", "Nevada", "Ohio", "Ohio", "Ohio", "Ohio"],
[2001, 2000, 2000, 2000, 2001, 2002]
])
righth = pd.DataFrame({"event1": pd.Series([0, 2, 4, 6, 8, 10], dtype="Int64",
index=righth_index), # 使用 MultiIndex
"event2": pd.Series([1, 3, 5, 7, 9, 11], dtype="Int64",
index=righth_index)})
pd.merge(lefth, righth, left_on=["key1", "key2"], right_index=True, how="outer") # 基于多个列和索引合并