Skip to content

Commit cf56698

Browse files
committed
DOC GH22897 Fix docstring of join in pandas/core/frame.py
1 parent f849134 commit cf56698

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

pandas/core/frame.py

+46-47
Original file line numberDiff line numberDiff line change
@@ -6440,6 +6440,8 @@ def append(self, other, ignore_index=False,
64406440
def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
64416441
sort=False):
64426442
"""
6443+
Append columns of another DataFrame.
6444+
64436445
Join columns with other DataFrame either on index or on a key
64446446
column. Efficiently Join multiple DataFrame objects by index at once by
64456447
passing a list.
@@ -6449,31 +6451,31 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
64496451
other : DataFrame, Series with name field set, or list of DataFrame
64506452
Index should be similar to one of the columns in this one. If a
64516453
Series is passed, its name attribute must be set, and that will be
6452-
used as the column name in the resulting joined DataFrame
6454+
used as the column name in the resulting joined DataFrame.
64536455
on : name, tuple/list of names, or array-like
64546456
Column or index level name(s) in the caller to join on the index
64556457
in `other`, otherwise joins index-on-index. If multiple
64566458
values given, the `other` DataFrame must have a MultiIndex. Can
64576459
pass an array as the join key if it is not already contained in
6458-
the calling DataFrame. Like an Excel VLOOKUP operation
6460+
the calling DataFrame. Like an Excel VLOOKUP operation.
64596461
how : {'left', 'right', 'outer', 'inner'}, default: 'left'
64606462
How to handle the operation of the two objects.
64616463
64626464
* left: use calling frame's index (or column if on is specified)
6463-
* right: use other frame's index
6465+
* right: use other frame's index.
64646466
* outer: form union of calling frame's index (or column if on is
6465-
specified) with other frame's index, and sort it
6466-
lexicographically
6467+
specified) with other frame's index, and sort it.
6468+
lexicographically.
64676469
* inner: form intersection of calling frame's index (or column if
64686470
on is specified) with other frame's index, preserving the order
6469-
of the calling's one
6471+
of the calling's one.
64706472
lsuffix : string
6471-
Suffix to use from left frame's overlapping columns
6473+
Suffix to use from left frame's overlapping columns.
64726474
rsuffix : string
6473-
Suffix to use from right frame's overlapping columns
6475+
Suffix to use from right frame's overlapping columns.
64746476
sort : boolean, default False
64756477
Order result DataFrame lexicographically by the join key. If False,
6476-
the order of the join key depends on the join type (how keyword)
6478+
the order of the join key depends on the join type (how keyword).
64776479
64786480
Notes
64796481
-----
@@ -6485,70 +6487,67 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
64856487
64866488
Examples
64876489
--------
6490+
>>> import pandas as pd
6491+
64886492
>>> caller = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
64896493
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
64906494
64916495
>>> caller
6492-
A key
6493-
0 A0 K0
6494-
1 A1 K1
6495-
2 A2 K2
6496-
3 A3 K3
6497-
4 A4 K4
6498-
5 A5 K5
6496+
key A
6497+
0 K0 A0
6498+
1 K1 A1
6499+
2 K2 A2
6500+
3 K3 A3
6501+
4 K4 A4
6502+
5 K5 A5
64996503
65006504
>>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
65016505
... 'B': ['B0', 'B1', 'B2']})
65026506
65036507
>>> other
6504-
B key
6505-
0 B0 K0
6506-
1 B1 K1
6507-
2 B2 K2
6508+
key B
6509+
0 K0 B0
6510+
1 K1 B1
6511+
2 K2 B2
65086512
65096513
Join DataFrames using their indexes.
65106514
65116515
>>> caller.join(other, lsuffix='_caller', rsuffix='_other')
6512-
6513-
>>> A key_caller B key_other
6514-
0 A0 K0 B0 K0
6515-
1 A1 K1 B1 K1
6516-
2 A2 K2 B2 K2
6517-
3 A3 K3 NaN NaN
6518-
4 A4 K4 NaN NaN
6519-
5 A5 K5 NaN NaN
6520-
6516+
key_caller A key_other B
6517+
0 K0 A0 K0 B0
6518+
1 K1 A1 K1 B1
6519+
2 K2 A2 K2 B2
6520+
3 K3 A3 NaN NaN
6521+
4 K4 A4 NaN NaN
6522+
5 K5 A5 NaN NaN
65216523
65226524
If we want to join using the key columns, we need to set key to be
65236525
the index in both caller and other. The joined DataFrame will have
65246526
key as its index.
65256527
65266528
>>> caller.set_index('key').join(other.set_index('key'))
6527-
6528-
>>> A B
6529-
key
6530-
K0 A0 B0
6531-
K1 A1 B1
6532-
K2 A2 B2
6533-
K3 A3 NaN
6534-
K4 A4 NaN
6535-
K5 A5 NaN
6529+
A B
6530+
key
6531+
K0 A0 B0
6532+
K1 A1 B1
6533+
K2 A2 B2
6534+
K3 A3 NaN
6535+
K4 A4 NaN
6536+
K5 A5 NaN
65366537
65376538
Another option to join using the key columns is to use the on
65386539
parameter. DataFrame.join always uses other's index but we can use any
65396540
column in the caller. This method preserves the original caller's
65406541
index in the result.
65416542
65426543
>>> caller.join(other.set_index('key'), on='key')
6543-
6544-
>>> A key B
6545-
0 A0 K0 B0
6546-
1 A1 K1 B1
6547-
2 A2 K2 B2
6548-
3 A3 K3 NaN
6549-
4 A4 K4 NaN
6550-
5 A5 K5 NaN
6551-
6544+
key A B
6545+
0 K0 A0 B0
6546+
1 K1 A1 B1
6547+
2 K2 A2 B2
6548+
3 K3 A3 NaN
6549+
4 K4 A4 NaN
6550+
5 K5 A5 NaN
65526551
65536552
See also
65546553
--------

0 commit comments

Comments
 (0)