Skip to content

Improved documentation for DataFrame.join #12193

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions doc/source/merging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,8 @@ DataFrame instance method, with the calling DataFrame being implicitly
considered the left object in the join.

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

Brief primer on merge methods (relational algebra)
Copy link
Member

Choose a reason for hiding this comment

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

Why did you shorten this?
I think the " joins on indexes by default" is very useful explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the shorter explanation is better:

index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
58 changes: 50 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4318,18 +4318,20 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
Series is passed, its name attribute must be set, and that will be
used as the column name in the resulting joined DataFrame
on : column name, tuple/list of column names, or array-like
Column(s) to use for joining, otherwise join on index. If multiples
Column(s) in the caller to join on the index in other,
otherwise joins index-on-index. If multiples
columns given, the passed DataFrame must have a MultiIndex. Can
pass an array as the join key if not already contained in the
calling DataFrame. Like an Excel VLOOKUP operation
how : {'left', 'right', 'outer', 'inner'}
How to handle indexes of the two objects. Default: 'left'
for joining on index, None otherwise

* left: use calling frame's index
* right: use input frame's index
* outer: form union of indexes
* inner: use intersection of indexes
How to handle the operation of the two objects. Default: 'left'

* left: use calling frame's index (or column if on is specified)
* right: use other frame's index
* outer: form union of calling frame's index (or column if on is
specified) with other frame's index
* inner: form intersection of calling frame's index (or column if
on is specified) with other frame's index
lsuffix : string
Suffix to use from left frame's overlapping columns
rsuffix : string
Expand All @@ -4343,6 +4345,46 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
on, lsuffix, and rsuffix options are not supported when passing a list
of DataFrame objects

Examples
--------
>>> caller = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})

>>> caller
A key
0 A0 K0
1 A1 K1
2 A2 K2
3 A3 K3
4 A4 K4
5 A5 K5

>>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
... 'B': ['B0', 'B1', 'B2']})

>>> other
B key
0 B0 K0
1 B1 K1
2 B2 K2

Perform a left join using caller's key column and other frame's index

>>> caller.join(other.set_index('key'), on='key', how='left',
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add this same example w/o using .set_index as well. (and w/o on), and indicate the difference between them.

Copy link
Contributor Author

@edublancas edublancas Apr 21, 2016

Choose a reason for hiding this comment

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

Just add this example? caller.join(other, how='left', lsuffix='_l', rsuffix='_r')

Copy link
Member

Choose a reason for hiding this comment

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

@jreback I don't think it is possible to not use set_index, as join always uses the index of other (which is actually really confusing ...)

@edublancas the lsuffix='_l', rsuffix='_r' is redundant in this case, so I would leave it out

Copy link
Contributor Author

@edublancas edublancas Apr 21, 2016

Choose a reason for hiding this comment

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

What about having just these two examples:

Perform a left join using caller's key column and other frame's index

caller.join(other.set_index('key'), on='key', how='left')

Set key as the index column on caller and other, then perform an index-on-index join.

caller.set_index('key').join(other.set_index('key'), how='left')

... lsuffix='_l', rsuffix='_r')

>>> A key B
0 A0 K0 B0
1 A1 K1 B1
2 A2 K2 B2
3 A3 K3 NaN
4 A4 K4 NaN
5 A5 K5 NaN

See also
--------
DataFrame.merge : For column(s)-on-columns(s) operations

Returns
-------
joined : DataFrame
Expand Down