Skip to content

Commit 8266cdc

Browse files
committed
DOC: improves DataFrame.join documentation
DOC: improves DataFrame.join documentation
1 parent de46056 commit 8266cdc

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

doc/source/merging.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,8 @@ DataFrame instance method, with the calling DataFrame being implicitly
558558
considered the left object in the join.
559559

560560
The related ``DataFrame.join`` method, uses ``merge`` internally for the
561-
index-on-index and index-on-column(s) joins, but *joins on indexes* by default
562-
rather than trying to join on common columns (the default behavior for
563-
``merge``). If you are joining on index, you may wish to use ``DataFrame.join``
564-
to save yourself some typing.
561+
index-on-index (by default) and column(s)-on-index join. If you are joining on
562+
index only, you may wish to use ``DataFrame.join`` to save yourself some typing.
565563

566564
Brief primer on merge methods (relational algebra)
567565
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pandas/core/frame.py

+50-8
Original file line numberDiff line numberDiff line change
@@ -4318,18 +4318,20 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
43184318
Series is passed, its name attribute must be set, and that will be
43194319
used as the column name in the resulting joined DataFrame
43204320
on : column name, tuple/list of column names, or array-like
4321-
Column(s) to use for joining, otherwise join on index. If multiples
4321+
Column(s) in the caller to join on the index in other,
4322+
otherwise joins index-on-index. If multiples
43224323
columns given, the passed DataFrame must have a MultiIndex. Can
43234324
pass an array as the join key if not already contained in the
43244325
calling DataFrame. Like an Excel VLOOKUP operation
43254326
how : {'left', 'right', 'outer', 'inner'}
4326-
How to handle indexes of the two objects. Default: 'left'
4327-
for joining on index, None otherwise
4328-
4329-
* left: use calling frame's index
4330-
* right: use input frame's index
4331-
* outer: form union of indexes
4332-
* inner: use intersection of indexes
4327+
How to handle the operation of the two objects. Default: 'left'
4328+
4329+
* left: use calling frame's index (or column if on is specified)
4330+
* right: use other frame's index
4331+
* outer: form union of calling frame's index (or column if on is
4332+
specified) with other frame's index
4333+
* inner: form intersection of calling frame's index (or column if
4334+
on is specified) with other frame's index
43334335
lsuffix : string
43344336
Suffix to use from left frame's overlapping columns
43354337
rsuffix : string
@@ -4343,6 +4345,46 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
43434345
on, lsuffix, and rsuffix options are not supported when passing a list
43444346
of DataFrame objects
43454347
4348+
Examples
4349+
--------
4350+
>>> caller = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
4351+
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
4352+
4353+
>>> caller
4354+
A key
4355+
0 A0 K0
4356+
1 A1 K1
4357+
2 A2 K2
4358+
3 A3 K3
4359+
4 A4 K4
4360+
5 A5 K5
4361+
4362+
>>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
4363+
... 'B': ['B0', 'B1', 'B2']})
4364+
4365+
>>> other
4366+
B key
4367+
0 B0 K0
4368+
1 B1 K1
4369+
2 B2 K2
4370+
4371+
Perform a left join using caller's key column and other frame's index
4372+
4373+
>>> caller.join(other.set_index('key'), on='key', how='left',
4374+
... lsuffix='_l', rsuffix='_r')
4375+
4376+
>>> A key B
4377+
0 A0 K0 B0
4378+
1 A1 K1 B1
4379+
2 A2 K2 B2
4380+
3 A3 K3 NaN
4381+
4 A4 K4 NaN
4382+
5 A5 K5 NaN
4383+
4384+
See also
4385+
--------
4386+
DataFrame.merge : For column(s)-on-columns(s) operations
4387+
43464388
Returns
43474389
-------
43484390
joined : DataFrame

0 commit comments

Comments
 (0)