pandas merge
时间: 2023-08-07 19:06:17 浏览: 132
Pandas merge is a function that allows you to combine two or more DataFrames based on a common column or index. It is similar to SQL join operations.
Here is an example of how to use the merge function in pandas:
```python
import pandas as pd
# Create two sample DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c']})
df2 = pd.DataFrame({'A': [3, 4, 5], 'C': ['x', 'y', 'z']})
# Merge the two DataFrames based on the 'A' column
merged_df = pd.merge(df1, df2, on='A')
print(merged_df)
```
This will result in the merged DataFrame with columns 'A', 'B', and 'C', where the rows with matching values in column 'A' are combined.
There are different types of merge operations, such as inner, outer, left, and right merge, which can be specified using the 'how' parameter in the merge function.
阅读全文