How do you merge two tables using the merge function in Python?

In Python, we can merge two tables using the merge() function from the pandas library.

The syntax of the merge() function is as follows: pandas.merge(left, right, how=’inner’, on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=(‘_x’, ‘_y’), copy=True, indicator=False, validate=None)

The left and right parameters refer to the two tables to be merged, the how parameter specifies the merging method, the on parameter specifies the column to be merged, the left_on and right_on parameters specify the column names of the left and right tables to be merged, and the left_index and right_index parameters specify whether the left and right tables should use index as the merging key.

Here is an example, let’s say we have two tables, df1 and df2:

import pandas as pd

# 创建示例表 df1 和 df2
df1 = pd.DataFrame({'id': [1, 2, 3], 'name': ['Alice', 'Bob', 'Charlie']})
df2 = pd.DataFrame({'id': [1, 2, 4], 'age': [20, 25, 30]})

# 使用 merge() 函数合并两个表
merged_df = pd.merge(df1, df2, on='id', how='inner')

print(merged_df)

The output result is:

   id    name  age
0   1   Alice   20
1   2     Bob   25

In the above example, we used the merge() function to combine tables df1 and df2 based on the id column, using an inner join. The final merged table, called merged_df, includes the id, name, and age columns.

bannerAds