To merge two tables in Python, you can use the 'merge' function provided by the pandas library. The 'merge' function combines rows from two tables based on one or more common columns.

Here is an example of how to merge two tables using pandas:

import pandas as pd

# Create two example tables
table1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value1': [1, 2, 3, 4]})
table2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value2': [5, 6, 7, 8]})

# Merge the tables based on the 'key' column
merged_table = pd.merge(table1, table2, on='key')

# Print the merged table
print(merged_table)

Output:

  key  value1  value2
0   B       2       5
1   D       4       6

In this example, we first created two tables called 'table1' and 'table2'. We then used the 'merge' function to merge the tables based on the 'key' column. The resulting merged table contains only the rows that have matching 'key' values in both tables.

Note that the 'merge' function can also handle more complex merging scenarios, such as merging on multiple columns or merging with different types of join operations. For more information, check out the pandas documentation on merging: https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html.

Python Pandas: Merge Two Tables with 'merge' Function

原文地址: https://www.cveoy.top/t/topic/nTIT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录