如何找出基准DataFrame中未出现在另一数据集的insight_id?
解决方案
你的代码逻辑刚好和需求相反——当前代码是筛选df_1中出现在df_2里的insight_id,要得到未出现的结果,需要对isin的结果取反,同时去重避免重复输出。
方法一:取反筛选+去重
import pandas as pd # 假设df_1和df_2已加载完成 # 筛选insight_id不在df_2中的行,提取对应列并去重 result = df_1.loc[~df_1['insight_id'].isin(df_2['insight_id']), 'insight_id'].drop_duplicates() # 转为目标格式输出 print(result.to_frame())
方法二:集合差集运算(高效简洁)
import pandas as pd # 提取两个数据集的insight_id集合,计算差集 unique_df1_ids = set(df_1['insight_id']) unique_df2_ids = set(df_2['insight_id']) diff_ids = unique_df1_ids - unique_df2_ids # 转为目标格式的DataFrame result = pd.DataFrame({'insight_id': list(diff_ids)}) print(result)
两种方法都能得到预期输出:
insight_id 0 ABC124
内容的提问来源于stack exchange,提问作者johnnydoe
相关产品推荐
相关产品推荐

