Pandas DataFrame join() Method – Be on the Right Side of Change (original) (raw)


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


FeFeel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd import numpy


The join() method joins columns by an index/key column. This method is great for joining multiple objects by their index.

The syntax for this method is as follows:

DataFrame.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False)

Parameter Description
other This parameter can be a DataFrame/Series/List and must be the same as a column in the existing object. If a Series, the name attribute needs to be set.
on This parameter is the column(s)/index(es) to join the index in other. If empty, the join uses index-on-index.
how This parameter can be one of the following options: – left: uses calling index (or column on, if used). – right: uses the index specified in the other parameter. – outer: creates union on calling index (or column, if on) with other index and sort. – inner: creates intersection on calling index (or column, if on) and preserves the order. – cross: creates the cartesian product from both and preserves the order of the left keys.
lsuffix This parameter is the suffix to use from the left DataFrame over-lapping column(s).
rsuffix This parameter is the suffix to use from the right DataFrame over-lapping column(s).
sort If False, the order of the join key depends on the how parameter selection.

For this example, we have two (2) DataFrames.

The first DataFrame contains student names and ages. The second DataFrame includes student classes and grades. The code below joins the two (2) DataFrames.

df = pd.DataFrame({'key': ['Lucy', 'Carl', 'Wanda'], 'age': [21, 18, 18]})

other = pd.DataFrame({key: ['Python', 'PHP', 'Java'], 'grade': [80, 56, 76]})

result = df.join(other, lsuffix='_caller', rsuffix='_other') print(result)

Output

| | key_caller | age | key_other | grade | | | ------------- | ----- | ---------- | ------ | -- | | 0 | Lucy | 21 | Python | 80 | | 1 | Carl | 18 | PHP | 56 | | 2 | Wanda | 18 | Java | 76 |

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.