Skip to content

Commit a66f2ea

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

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

pandas/core/frame.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -4368,10 +4368,40 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
43684368
1 B1 K1
43694369
2 B2 K2
43704370
4371-
Perform a left join using caller's key column and other frame's index
4371+
Join DataFrames using their indexes.
43724372
4373-
>>> caller.join(other.set_index('key'), on='key', how='left',
4374-
... lsuffix='_l', rsuffix='_r')
4373+
>>> caller.join(other, lsuffix='_caller', rsuffix='_other')
4374+
4375+
>>> A key_caller B key_other
4376+
0 A0 K0 B0 K0
4377+
1 A1 K1 B1 K1
4378+
2 A2 K2 B2 K2
4379+
3 A3 K3 NaN NaN
4380+
4 A4 K4 NaN NaN
4381+
5 A5 K5 NaN NaN
4382+
4383+
4384+
If we want to join using the key columns, we need to set key to be
4385+
the index in both caller and other. The joined DataFrame will have
4386+
key as its index.
4387+
4388+
>>> caller.set_index('key').join(other.set_index('key'))
4389+
4390+
>>> A B
4391+
key
4392+
K0 A0 B0
4393+
K1 A1 B1
4394+
K2 A2 B2
4395+
K3 A3 NaN
4396+
K4 A4 NaN
4397+
K5 A5 NaN
4398+
4399+
Another option to join using the key columns is to use the on
4400+
parameter. DataFrame.join always uses other's index but we can use any
4401+
column in the caller. This method preserves the original caller's
4402+
index in the result.
4403+
4404+
>>> caller.join(other.set_index('key'), on='key')
43754405
43764406
>>> A key B
43774407
0 A0 K0 B0
@@ -4381,6 +4411,7 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
43814411
4 A4 K4 NaN
43824412
5 A5 K5 NaN
43834413
4414+
43844415
See also
43854416
--------
43864417
DataFrame.merge : For column(s)-on-columns(s) operations

0 commit comments

Comments
 (0)