如何合并行数不同的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:
- 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']))
- 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)]
- 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:
- 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 )
- 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')]
- Restore the original column names (merge adds
_x/_ysuffixes):
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

