Skip to content

Commit 9198f35

Browse files
DOC: Improve code example for DataFrame.join (#44735)
1 parent daac93c commit 9198f35

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/core/frame.py

+23
Original file line numberDiff line numberDiff line change
@@ -9183,6 +9183,29 @@ def join(
91839183
3 K3 A3 NaN
91849184
4 K4 A4 NaN
91859185
5 K5 A5 NaN
9186+
9187+
Using non-unique key values shows how they are matched.
9188+
9189+
>>> df = pd.DataFrame({'key': ['K0', 'K1', 'K1', 'K3', 'K0', 'K1'],
9190+
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
9191+
9192+
>>> df
9193+
key A
9194+
0 K0 A0
9195+
1 K1 A1
9196+
2 K1 A2
9197+
3 K3 A3
9198+
4 K0 A4
9199+
5 K1 A5
9200+
9201+
>>> df.join(other.set_index('key'), on='key')
9202+
key A B
9203+
0 K0 A0 B0
9204+
1 K1 A1 B1
9205+
2 K1 A2 B1
9206+
3 K3 A3 NaN
9207+
4 K0 A4 B0
9208+
5 K1 A5 B1
91869209
"""
91879210
return self._join_compat(
91889211
other, on=on, how=how, lsuffix=lsuffix, rsuffix=rsuffix, sort=sort

0 commit comments

Comments
 (0)