@@ -6440,6 +6440,8 @@ def append(self, other, ignore_index=False,
6440
6440
def join (self , other , on = None , how = 'left' , lsuffix = '' , rsuffix = '' ,
6441
6441
sort = False ):
6442
6442
"""
6443
+ Append columns of another DataFrame.
6444
+
6443
6445
Join columns with other DataFrame either on index or on a key
6444
6446
column. Efficiently Join multiple DataFrame objects by index at once by
6445
6447
passing a list.
@@ -6449,31 +6451,31 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
6449
6451
other : DataFrame, Series with name field set, or list of DataFrame
6450
6452
Index should be similar to one of the columns in this one. If a
6451
6453
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.
6453
6455
on : name, tuple/list of names, or array-like
6454
6456
Column or index level name(s) in the caller to join on the index
6455
6457
in `other`, otherwise joins index-on-index. If multiple
6456
6458
values given, the `other` DataFrame must have a MultiIndex. Can
6457
6459
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.
6459
6461
how : {'left', 'right', 'outer', 'inner'}, default: 'left'
6460
6462
How to handle the operation of the two objects.
6461
6463
6462
6464
* 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.
6464
6466
* 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.
6467
6469
* inner: form intersection of calling frame's index (or column if
6468
6470
on is specified) with other frame's index, preserving the order
6469
- of the calling's one
6471
+ of the calling's one.
6470
6472
lsuffix : string
6471
- Suffix to use from left frame's overlapping columns
6473
+ Suffix to use from left frame's overlapping columns.
6472
6474
rsuffix : string
6473
- Suffix to use from right frame's overlapping columns
6475
+ Suffix to use from right frame's overlapping columns.
6474
6476
sort : boolean, default False
6475
6477
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).
6477
6479
6478
6480
Notes
6479
6481
-----
@@ -6485,70 +6487,67 @@ def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
6485
6487
6486
6488
Examples
6487
6489
--------
6490
+ >>> import pandas as pd
6491
+
6488
6492
>>> caller = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
6489
6493
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
6490
6494
6491
6495
>>> 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
6499
6503
6500
6504
>>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
6501
6505
... 'B': ['B0', 'B1', 'B2']})
6502
6506
6503
6507
>>> 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
6508
6512
6509
6513
Join DataFrames using their indexes.
6510
6514
6511
6515
>>> 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
6521
6523
6522
6524
If we want to join using the key columns, we need to set key to be
6523
6525
the index in both caller and other. The joined DataFrame will have
6524
6526
key as its index.
6525
6527
6526
6528
>>> 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
6536
6537
6537
6538
Another option to join using the key columns is to use the on
6538
6539
parameter. DataFrame.join always uses other's index but we can use any
6539
6540
column in the caller. This method preserves the original caller's
6540
6541
index in the result.
6541
6542
6542
6543
>>> 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
6552
6551
6553
6552
See also
6554
6553
--------
0 commit comments