You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中基于共同列合并DataFrame并筛选特定行?

Hey there! Let's tackle your two Pandas data handling questions—these are super common tasks, so I’ll walk you through clear, actionable solutions.

1. Can I merge two DataFrames on a common column AND select specific rows in one go?

Absolutely! You’ve got two efficient approaches here, depending on your data size and preference:

It’s faster to narrow down your data before merging, since you’ll be working with fewer rows overall. Here’s how:

import pandas as pd

# Sample DataFrames
df1 = pd.DataFrame({'user_id': [1,2,3,4,5], 'username': ['alice','bob','charlie','dave','eve']})
df2 = pd.DataFrame({'user_id': [2,3,5,6,7], 'score': [85,92,78,95,88]})

# Define the specific rows you want to keep (e.g., user_ids 3 and 5)
target_users = [3,5]

# Filter each DataFrame first
filtered_df1 = df1[df1['user_id'].isin(target_users)]
filtered_df2 = df2[df2['user_id'].isin(target_users)]

# Now merge on the common column
merged_filtered = pd.merge(filtered_df1, filtered_df2, on='user_id', how='inner')

Option 2: Merge first, then filter

If you need the full merged dataset for other tasks too, you can filter after merging:

# Merge the full DataFrames first
full_merged = pd.merge(df1, df2, on='user_id', how='inner')

# Then filter the specific rows
filtered_result = full_merged[full_merged['user_id'].isin(target_users)]

Stick with Option 1 if your datasets are large—it’ll save you memory and processing time.

2. How do I select specific rows from a merged DataFrame to create a new one?

Once you’ve got your merged DataFrame, there are several flexible ways to extract the rows you need:

Filter by column values (most common)

Use boolean indexing to pick rows where a column matches your criteria:

# Assume merged_df is your already merged DataFrame
# Select rows where score is above 90 OR username is 'eve'
new_df = merged_df[(merged_df['score'] > 90) | (merged_df['username'] == 'eve')]

Use isin() for multiple values

If you’re targeting multiple specific values in a column:

# Select rows where user_id is 2 or 5
new_df = merged_df[merged_df['user_id'].isin([2,5])]

Use query() for cleaner syntax

For more readable conditions, especially with complex logic:

# Select rows where score is between 80 and 90, excluding user_id 2
new_df = merged_df.query("80 < score < 90 and user_id != 2")

Select by row index

If you know the exact positions of the rows you want, use iloc:

# Select the 1st and 3rd rows (remember, Pandas uses 0-based indexing)
new_df = merged_df.iloc[[1,3]]

All these methods return a new DataFrame—your original merged data stays untouched unless you explicitly overwrite it.

内容的提问来源于stack exchange,提问作者ASHWIN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:57:35