Skip to content

Commit a4886ad

Browse files
ryankarlosroberthdevries
authored andcommitted
DOC: Add example for multiindex series and dataframe merge (pandas-dev#32068)
1 parent e7eda71 commit a4886ad

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

doc/source/user_guide/merging.rst

+21
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,27 @@ either the left or right tables, the values in the joined table will be
724724
labels=['left', 'right'], vertical=False);
725725
plt.close('all');
726726
727+
You can merge a mult-indexed Series and a DataFrame, if the names of
728+
the MultiIndex correspond to the columns from the DataFrame. Transform
729+
the Series to a DataFrame using :meth:`Series.reset_index` before merging,
730+
as shown in the following example.
731+
732+
.. ipython:: python
733+
734+
df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]})
735+
df
736+
737+
ser = pd.Series(
738+
["a", "b", "c", "d", "e", "f"],
739+
index=pd.MultiIndex.from_arrays(
740+
[["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"]
741+
),
742+
)
743+
ser
744+
745+
result = pd.merge(df, ser.reset_index(), on=['Let', 'Num'])
746+
747+
727748
Here is another example with duplicate join keys in DataFrames:
728749

729750
.. ipython:: python

0 commit comments

Comments
 (0)