|
| 1 | +--- |
| 2 | +Title: 'join()' |
| 3 | +Description: 'Combines columns from another DataFrame into the calling DataFrame based on the index or a key column.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'DataFrame' |
| 9 | + - 'Join' |
| 10 | + - 'Pandas' |
| 11 | + - 'Python' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In Pandas, **`DataFrame.join()`** combines columns from another DataFrame (or multiple DataFrames) into the calling DataFrame based on the index or a key column. It’s mainly used for merging DataFrames with different sets of columns but shared row indices. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +DataFrame.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False, validate=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `other`: Objects to join with the caller DataFrame. |
| 28 | +- `on`: Column(s) in the caller DataFrame to join on; must match the index in `other` if provided. |
| 29 | +- `how`: Type of join to perform — 'left', 'right', 'outer', or 'inner'. |
| 30 | +- `lsuffix`: Suffix to add to overlapping column names from the left DataFrame. |
| 31 | +- `rsuffix`: Suffix to add to overlapping column names from the right DataFrame. |
| 32 | +- `sort`: Sort the result DataFrame by the join keys if True. |
| 33 | +- `validate`: Checks if the join is of a specific type ('one_to_one', 'one_to_many', 'many_to_one', 'many_to_many'). |
| 34 | + |
| 35 | +**Return value:** |
| 36 | + |
| 37 | +Returns a new DataFrame with columns of both joined DataFrames combined according to the join logic. |
| 38 | + |
| 39 | +## Example |
| 40 | + |
| 41 | +In this example, a list of words is joined into a single string separated by spaces: |
| 42 | + |
| 43 | +```py |
| 44 | +words = ['Codecademy', 'is', 'awesome'] |
| 45 | +result = ' '.join(words) |
| 46 | +print(result) |
| 47 | +``` |
| 48 | + |
| 49 | +This code produces the following output: |
| 50 | + |
| 51 | +```shell |
| 52 | +Codecademy is awesome |
| 53 | +``` |
| 54 | + |
| 55 | +## Codebyte Example |
| 56 | + |
| 57 | +In this example, a list of characters is joined into a single string separated by commas: |
| 58 | + |
| 59 | +```codebyte/python |
| 60 | +print(','.join(['a', 'b', 'c'])) |
| 61 | +``` |
0 commit comments