-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Support merging DataFrames on a combo of columns and index levels (GH 14355) #17484
Changes from 33 commits
da94fdb
f8c8c53
368844a
1c4699e
ac1189b
d90ed78
27b2d25
de6f4b1
39d0bba
5b1b100
dfc6cf7
03e3c2e
bf5d349
7da39aa
f5a16ff
aa099ea
3be43a4
b655e30
b7e2cc2
e9f02b1
0cd4ef5
e029f7b
fdddbd3
89061b9
090b3e8
47ff8b8
59f2dce
4c4dbd0
1d7e570
dd289a6
313d2c3
0b0397b
bc53bef
cd17c42
1a4e3e4
a49012c
6fd9760
f7e04f5
cf8e654
e874f04
13ce87c
b5cb4c1
f3b95fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,37 @@ New features | |
- | ||
- | ||
|
||
.. _whatsnew_0220.enhancements.merge_on_columns_and_levels: | ||
|
||
Merging on a combination of columns and index levels | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Strings passed to :meth:`DataFrame.merge` as the ``on``, ``left_on``, and ``right_on`` | ||
parameters may now refer to either column names or index level names. | ||
This enables merging ``DataFrame`` instances on a combination of index levels | ||
and columns without resetting indexes. See the :ref:`Merge on columns and | ||
levels <merging.merge_on_columns_and_levels>` documentation section. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue number here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue reference moved to the end of paragraph |
||
(:issue:`14355`) | ||
|
||
.. ipython:: python | ||
|
||
left_index = pd.Index(['K0', 'K0', 'K1', 'K2'], name='key1') | ||
|
||
left = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'], | ||
'B': ['B0', 'B1', 'B2', 'B3'], | ||
'key2': ['K0', 'K1', 'K0', 'K1']}, | ||
index=left_index) | ||
|
||
right_index = pd.Index(['K0', 'K1', 'K2', 'K2'], name='key1') | ||
|
||
right = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'], | ||
'D': ['D0', 'D1', 'D2', 'D3'], | ||
'key2': ['K0', 'K0', 'K0', 'K1']}, | ||
index=right_index) | ||
|
||
left.merge(right, on=['key1', 'key2']) | ||
|
||
|
||
.. _whatsnew_0220.enhancements.other: | ||
|
||
Other Enhancements | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,16 +147,17 @@ | |
* inner: use intersection of keys from both frames, similar to a SQL inner | ||
join; preserve the order of the left keys | ||
on : label or list | ||
Field names to join on. Must be found in both DataFrames. If on is | ||
None and not merging on indexes, then it merges on the intersection of | ||
the columns by default. | ||
Column or index level names to join on. These must be found in both | ||
DataFrames. If on is None and not merging on indexes then this defaults to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put single backticks around 'on' ? (that is typically done for parameters of the function) |
||
the intersection of the columns in both DataFrames. | ||
left_on : label or list, or array-like | ||
Field names to join on in left DataFrame. Can be a vector or list of | ||
vectors of the length of the DataFrame to use a particular vector as | ||
the join key instead of columns | ||
Column or index level names to join on in the left DataFrame. Can also | ||
be a vector or list of vectors of the length of the left DataFrame. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now you are changing this anyway, can you change vector with array ? |
||
These vectors are treated as though they are columns. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think 'as if' is easier language than 'as though' for non-native speakers |
||
right_on : label or list, or array-like | ||
Field names to join on in right DataFrame or vector/list of vectors per | ||
left_on docs | ||
Column or index level names to join on in the right DataFrame. Can also | ||
be a vector or list of vectors of the length of the right DataFrame. | ||
These vectors are treated as though they are columns. | ||
left_index : boolean, default False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, maybe in a Note section |
||
Use the index from the left DataFrame as the join key(s). If it is a | ||
MultiIndex, the number of keys in the other DataFrame (either the index | ||
|
@@ -195,6 +196,11 @@ | |
|
||
.. versionadded:: 0.21.0 | ||
|
||
Note | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note -> Notes (section names are fixed by numpydoc) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs update here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching this @jreback. Fixed |
||
---- | ||
Support for specifying index levels as the ``on``, ``left_on``, and | ||
``right_on`` parameters was added in version 0.22.0. | ||
|
||
Examples | ||
-------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an you add a comment (here and left_on/right_on) that index level merging is new in 0.22.0 (or maybe in a Note section below)