pandas
  1. pandas-dataframejoin

DataFrame.join() - ( Pandas DataFrame Basics )

Heading h2

Syntax

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

Example

import pandas as pd

df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': [1, 2, 3, 4]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': [5, 6, 7, 8]})

df3 = df1.join(df2.set_index('key'), on='key')

Output

  key  value_x  value_y
0   A        1     NaN
1   B        2     5.0
2   C        3     NaN
3   D        4     6.0

Explanation

DataFrame.join() method is used to combine two or more DataFrames based on the values of a common column or index. It is similar to SQL join operations.

In the example above, we have two DataFrames, df1 and df2, with one common column, key. We use df2.set_index('key') to make the key column the index of df2, and then we join df1 with df2 using df1.join(df2.set_index('key'), on='key').

The resulting DataFrame has three columns, key, value_x, and value_y, where value_x and value_y are the corresponding columns from df1 and df2, respectively.

Use

DataFrame.join() is used to combine multiple DataFrames on the basis of common columns or indices. It can be used to merge datasets for further analysis or to combine different sources of data into a single DataFrame.

Important Points

  • DataFrame.join() method is used to combine two or more DataFrames on the basis of common columns or indices.
  • It is similar to SQL join operations.
  • It supports different join types including inner, outer, left, and right join.
  • By default, how='left' which performs a left join.

Summary

In conclusion, DataFrame.join() is an important method in Pandas that allows us to combine multiple DataFrames on the basis of common columns or indices. It can be used to merge datasets for further analysis or to combine different sources of data into a single DataFrame. It supports different join types including inner, outer, left, and right join.

Published on: