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

如何合并行数不同的DataFrame,提取LikeColumn值不等的行?

解决方案:筛选两个DataFrame中"LikeColumn"值不重叠的行

Hey Matt, great question! Since you're working with two DataFrames that share the same column names and count (but different row counts), here are a couple of straightforward ways to build your df3 with rows where the "LikeColumn" values don't overlap between the two dfs:


方法1:基于集合交集的筛选(简洁高效)

This is my go-to for this kind of task—it's quick and easy to follow:

  1. First, find all the "LikeColumn" values that exist in both DataFrames:
import pandas as pd

# Calculate common values in LikeColumn across both dfs
common_likes = set(df1['LikeColumn']).intersection(set(df2['LikeColumn']))
  1. Filter each DataFrame to keep only rows where "LikeColumn" isn't in that common set:
# Filter df1: keep rows where LikeColumn is NOT in common values
df1_filtered = df1[~df1['LikeColumn'].isin(common_likes)]
# Filter df2 the same way
df2_filtered = df2[~df2['LikeColumn'].isin(common_likes)]
  1. Combine the filtered results into your final df3:
df3 = pd.concat([df1_filtered, df2_filtered]).reset_index(drop=True)

The reset_index(drop=True) just cleans up the index so it's sequential instead of keeping the original indices from df1 and df2.


方法2:使用Outer Merge(适合追踪行来源)

If you also want to know which original DataFrame each row in df3 came from, this method is perfect:

  1. Do an outer merge on "LikeColumn" and add an indicator column to track source:
merged_df = pd.merge(
    df1, 
    df2, 
    on='LikeColumn', 
    how='outer', 
    indicator=True  # Adds a column showing which df the row came from
)
  1. Filter rows that only exist in df1 (left_only) or only exist in df2 (right_only):
filtered_rows = merged_df[(merged_df['_merge'] == 'left_only') | (merged_df['_merge'] == 'right_only')]
  1. Restore the original column names (merge adds _x/_y suffixes):
df3 = pd.DataFrame()
for col in df1.columns:
    # Fill missing values from one df with values from the other
    df3[col] = filtered_rows[f'{col}_x'].fillna(filtered_rows[f'{col}_y'])

# Optional: Keep the _merge column if you want to track row origin
# df3['source_df'] = filtered_rows['_merge'].replace({'left_only': 'df1', 'right_only': 'df2'})

Both methods will give you exactly what you need: a df3 containing all rows where the "LikeColumn" value doesn't appear in the other DataFrame. Pick whichever fits your workflow better!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:39:20