Pandas DataFrame join() Method


Preparation

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

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

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

DataFrame join()

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)
ParameterDescription
otherThis 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.
onThis parameter is the column(s)/index(es) to join the index in other. If empty, the join uses index-on-index.
howThis 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.
lsuffixThis parameter is the suffix to use from the left DataFrame over-lapping column(s).
rsuffixThis parameter is the suffix to use from the right DataFrame over-lapping column(s).
sortIf 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)
  • Line [1] creates a DataFrame and saves it to df.
  • Line [2] creates a DataFrame and saves it to other.
  • Line [3] joins the two (2) DataFrames and appends suffixes to each column name. This output saves to result.
  • Line [4] outputs the result to the terminal.

Output

 key_caller agekey_other grade
0Lucy  21   Python    80
1Carl  18      PHP    56
2Wanda  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.