@@ -518,14 +518,16 @@ standard database join operations between DataFrame objects:
518518
519519- ``left ``: A DataFrame object
520520- ``right ``: Another DataFrame object
521- - ``on ``: Columns ( names) to join on. Must be found in both the left and
522- right DataFrame objects. If not passed and ``left_index `` and
521+ - ``on ``: Column or index level names to join on. Must be found in both the left
522+ and right DataFrame objects. If not passed and ``left_index `` and
523523 ``right_index `` are ``False ``, the intersection of the columns in the
524524 DataFrames will be inferred to be the join keys
525- - ``left_on ``: Columns from the left DataFrame to use as keys. Can either be
526- column names or arrays with length equal to the length of the DataFrame
527- - ``right_on ``: Columns from the right DataFrame to use as keys. Can either be
528- column names or arrays with length equal to the length of the DataFrame
525+ - ``left_on ``: Columns or index levels from the left DataFrame to use as
526+ keys. Can either be column names, index level names, or arrays with length
527+ equal to the length of the DataFrame
528+ - ``right_on ``: Columns or index levels from the right DataFrame to use as
529+ keys. Can either be column names, index level names, or arrays with length
530+ equal to the length of the DataFrame
529531- ``left_index ``: If ``True ``, use the index (row labels) from the left
530532 DataFrame as its join key(s). In the case of a DataFrame with a MultiIndex
531533 (hierarchical), the number of levels must match the number of join keys
@@ -563,6 +565,10 @@ standard database join operations between DataFrame objects:
563565
564566 .. versionadded :: 0.21.0
565567
568+ .. note ::
569+
570+ Support for specifying index levels as the ``on ``, ``left_on ``, and
571+ ``right_on `` parameters was added in version 0.22.0.
566572
567573The return type will be the same as ``left ``. If ``left `` is a ``DataFrame ``
568574and ``right `` is a subclass of DataFrame, the return type will still be
@@ -1121,6 +1127,56 @@ This is not Implemented via ``join`` at-the-moment, however it can be done using
11211127 labels = [' left' , ' right' ], vertical = False );
11221128 plt.close(' all' );
11231129
1130+ .. _merging.merge_on_columns_and_levels :
1131+
1132+ Merging on a combination of columns and index levels
1133+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1134+
1135+ .. versionadded :: 0.22
1136+
1137+ Strings passed as the ``on ``, ``left_on ``, and ``right_on `` parameters
1138+ may refer to either column names or index level names. This enables merging
1139+ ``DataFrame `` instances on a combination of index levels and columns without
1140+ resetting indexes.
1141+
1142+ .. ipython :: python
1143+
1144+ left_index = pd.Index([' K0' , ' K0' , ' K1' , ' K2' ], name = ' key1' )
1145+
1146+ left = pd.DataFrame({' A' : [' A0' , ' A1' , ' A2' , ' A3' ],
1147+ ' B' : [' B0' , ' B1' , ' B2' , ' B3' ],
1148+ ' key2' : [' K0' , ' K1' , ' K0' , ' K1' ]},
1149+ index = left_index)
1150+
1151+ right_index = pd.Index([' K0' , ' K1' , ' K2' , ' K2' ], name = ' key1' )
1152+
1153+ right = pd.DataFrame({' C' : [' C0' , ' C1' , ' C2' , ' C3' ],
1154+ ' D' : [' D0' , ' D1' , ' D2' , ' D3' ],
1155+ ' key2' : [' K0' , ' K0' , ' K0' , ' K1' ]},
1156+ index = right_index)
1157+
1158+ result = left.merge(right, on = [' key1' , ' key2' ])
1159+
1160+ .. ipython :: python
1161+ :suppress:
1162+
1163+ @savefig merge_on_index_and_column.png
1164+ p.plot([left, right], result,
1165+ labels = [' left' , ' right' ], vertical = False );
1166+ plt.close(' all' );
1167+
1168+ .. note ::
1169+
1170+ When DataFrames are merged on a string that matches an index level in both
1171+ frames, the index level is preserved as an index level in the resulting
1172+ DataFrame.
1173+
1174+ .. note ::
1175+
1176+ If a string matches both a column name and an index level name, then a
1177+ warning is issued and the column takes precedence. This will result in an
1178+ ambiguity error in a future version.
1179+
11241180Overlapping value columns
11251181~~~~~~~~~~~~~~~~~~~~~~~~~
11261182
0 commit comments