how to find the common values between two different sizes of dataframe
时间: 2024-04-12 22:30:14 浏览: 103
To find the common values between two different-sized DataFrames in Python, you can use the `merge()` function from the pandas library. The `merge()` function combines two DataFrames based on common values in specified columns. Here's an example:
```python
import pandas as pd
# Create two example DataFrames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [2, 3, 4], 'C': [7, 8, 9]})
# Find common values between the two DataFrames
common_values = pd.merge(df1, df2, on='A')
print(common_values)
```
Output:
```
A B C
0 2 5 7
1 3 6 8
```
In the above code, we merge `df1` and `df2` based on the common values in column `'A'`. The result is a new DataFrame `common_values` that contains rows with common values in column `'A'`. The resulting DataFrame includes the columns from both `df1` and `df2`.
阅读全文