From b267d150cd33c631f3fc975c5c6dafef7ead50db Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 18 Feb 2020 00:02:44 +0000 Subject: [PATCH 1/8] Add example in docs for multiindex series and dataframe merge --- doc/source/user_guide/merging.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index 8fdcd8d281a41..e055a77f02486 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -716,6 +716,10 @@ either the left or right tables, the values in the joined table will be result = pd.merge(left, right, how='inner', on=['key1', 'key2']) +To join a Series with a MultiIndex and a DataFrame, using the levels of the +MultiIndex and columns from the DataFrame, transform the Series to a DataFrame +using :meth:`Series.reset_index`. + .. ipython:: python :suppress: @@ -724,6 +728,25 @@ either the left or right tables, the values in the joined table will be labels=['left', 'right'], vertical=False); plt.close('all'); +.. ipython:: python + + df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) + df + + # The series has a multi-index with levels corresponding to columns in the DataFrame we want to merge with + 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 + + # Convert the Series to a DataFrame and merge + pd.merge(df, ser.reset_index(), on=['Let', 'Num']) + type(df2) + + # Now we merge the DataFrames + pd.merge(df, df2, on=['Let', 'Num']) + Here is another example with duplicate join keys in DataFrames: .. ipython:: python From cdd75ff9df4a16ec74fc44b29d23a3ab058cd338 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 18 Feb 2020 00:10:29 +0000 Subject: [PATCH 2/8] shift description to correct location --- doc/source/user_guide/merging.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index e055a77f02486..71a7faff9c6a2 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -716,10 +716,6 @@ either the left or right tables, the values in the joined table will be result = pd.merge(left, right, how='inner', on=['key1', 'key2']) -To join a Series with a MultiIndex and a DataFrame, using the levels of the -MultiIndex and columns from the DataFrame, transform the Series to a DataFrame -using :meth:`Series.reset_index`. - .. ipython:: python :suppress: @@ -728,6 +724,10 @@ using :meth:`Series.reset_index`. labels=['left', 'right'], vertical=False); plt.close('all'); +To join a Series with a MultiIndex and a DataFrame, using the levels of the +MultiIndex and columns from the DataFrame, transform the Series to a DataFrame +using :meth:`Series.reset_index`. + .. ipython:: python df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) From a16d814ca785cb2c332519fcbd87362ee110aa62 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 18 Feb 2020 00:42:07 +0000 Subject: [PATCH 3/8] linting --- doc/source/user_guide/merging.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index 71a7faff9c6a2..ebce7679ce9a4 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -733,11 +733,11 @@ using :meth:`Series.reset_index`. df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) df - # The series has a multi-index with levels corresponding to columns in the DataFrame we want to merge with - 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'] - ) + # The series has a multi-index with levels corresponding to columns in + # the DataFrame we want to merge with + 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 # Convert the Series to a DataFrame and merge From 8ba6fdb2aca08fec08c7bd45aea490fba3180de0 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 24 Feb 2020 01:06:21 +0000 Subject: [PATCH 4/8] syntax error and linting --- doc/source/user_guide/merging.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index ebce7679ce9a4..98c5868291d9c 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -735,13 +735,15 @@ using :meth:`Series.reset_index`. # The series has a multi-index with levels corresponding to columns in # the DataFrame we want to merge with - 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 = 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 # Convert the Series to a DataFrame and merge - pd.merge(df, ser.reset_index(), on=['Let', 'Num']) + df2 = pd.merge(df, ser.reset_index(), on=['Let', 'Num']) type(df2) # Now we merge the DataFrames From c4ffbe302fa974de9e9b7df7656efc87cb865793 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 24 Feb 2020 01:51:33 +0000 Subject: [PATCH 5/8] Linting indentation fix --- doc/source/user_guide/merging.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index 98c5868291d9c..88e89eeaaf92d 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -735,11 +735,11 @@ using :meth:`Series.reset_index`. # The series has a multi-index with levels corresponding to columns in # the DataFrame we want to merge with - 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 = 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 # Convert the Series to a DataFrame and merge From 9299bddd7bb21b9b4b6a26e34dc964d517f29633 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 24 Feb 2020 22:32:14 +0000 Subject: [PATCH 6/8] Simplify example and add description outside block --- doc/source/user_guide/merging.rst | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index 88e89eeaaf92d..ff85b4b4f5dbb 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -724,30 +724,26 @@ either the left or right tables, the values in the joined table will be labels=['left', 'right'], vertical=False); plt.close('all'); -To join a Series with a MultiIndex and a DataFrame, using the levels of the -MultiIndex and columns from the DataFrame, transform the Series to a DataFrame -using :meth:`Series.reset_index`. +You can merge a MultiIndex Series and a DataFrame, if the levels 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 - # The series has a multi-index with levels corresponding to columns in - # the DataFrame we want to merge with - 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 = 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 - # Convert the Series to a DataFrame and merge - df2 = pd.merge(df, ser.reset_index(), on=['Let', 'Num']) - type(df2) + pd.merge(df, ser.reset_index(), on=['Let', 'Num']) - # Now we merge the DataFrames - pd.merge(df, df2, on=['Let', 'Num']) Here is another example with duplicate join keys in DataFrames: From ff20642e7778faf1135d4857fd675fba09fcbaed Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 24 Feb 2020 22:38:50 +0000 Subject: [PATCH 7/8] add result variable for consistency with other examples --- doc/source/user_guide/merging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index ff85b4b4f5dbb..6de43af371661 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -742,7 +742,7 @@ as shown in the following example. ) ser - pd.merge(df, ser.reset_index(), on=['Let', 'Num']) + result = pd.merge(df, ser.reset_index(), on=['Let', 'Num']) Here is another example with duplicate join keys in DataFrames: From 30e631a34b041d3ede6192ffddb7151ee3a3233d Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Thu, 27 Feb 2020 19:54:42 +0000 Subject: [PATCH 8/8] minor change to description --- doc/source/user_guide/merging.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/user_guide/merging.rst b/doc/source/user_guide/merging.rst index 6de43af371661..8302b5c5dea60 100644 --- a/doc/source/user_guide/merging.rst +++ b/doc/source/user_guide/merging.rst @@ -724,7 +724,7 @@ 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 MultiIndex Series and a DataFrame, if the levels of +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.