Skip to content

DOC: Add example for multiindex series and dataframe merge #32068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
21 changes: 21 additions & 0 deletions doc/source/user_guide/merging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,27 @@ either the left or right tables, the values in the joined table will be
labels=['left', 'right'], vertical=False);
plt.close('all');

You can merge a mult-indexed Series and a DataFrame, if the names of
the MultiIndex correspond to the columns from the DataFrame. Transform
the Series to a DataFrame using :meth:`Series.reset_index` before merging,
as shown in the following example.

.. ipython:: python

df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]})
df

ser = pd.Series(
["a", "b", "c", "d", "e", "f"],
index=pd.MultiIndex.from_arrays(
[["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"]
),
)
ser

result = pd.merge(df, ser.reset_index(), on=['Let', 'Num'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just

   pd.merge(df, ser.reset_index(), on=['Let', 'Num'])

Copy link
Contributor Author

@ryankarlos ryankarlos Feb 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok just fixed this in PR #32370 - looking at the rendered docs - how are the diagrams of the df displayed ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok just fixed this in PR #32370

thanks

how are the diagrams of the df displayed ?

since we have a Series and a DataFrame, that format is probably not applicable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - thanks. PR #32370 is all green now.



Here is another example with duplicate join keys in DataFrames:

.. ipython:: python
Expand Down