PythonPandas:如何基于单个列查找两个 Dataframe 之间的差异

cgvd09ve  于 2022-11-27  发布在  Python
关注(0)|答案(1)|浏览(169)

我有两个 Dataframe

df1 = pd.DataFrame({
    'Date':['2013-11-24','2013-11-24','2013-11-25','2013-11-25'],
    'Fruit':['Banana','Orange','Apple','Celery'],
    'Num':[22.1,8.6,7.6,10.2],
    'Color':['Yellow','Orange','Green','Green'],
    })
print(df1)
         Date   Fruit   Num   Color
0  2013-11-24  Banana  22.1  Yellow
1  2013-11-24  Orange   8.6  Orange
2  2013-11-25   Apple   7.6   Green
3  2013-11-25  Celery  10.2   Green

df2 = pd.DataFrame({
    'Date':['2013-11-25','2013-11-25','2013-11-25','2013-11-25','2013-11-25','2013-11-25'],
    'Fruit':['Banana','Orange','Apple','Celery','X','Y'],
    'Num':[22.1,8.6,7.6,10.2,22.1,8.6],
    'Color':['Yellow','Orange','Green','Green','Red','Orange'],
    })
print(df2)
         Date   Fruit   Num   Color
0  2013-11-25  Banana  22.1  Yellow
1  2013-11-25  Orange   8.6  Orange
2  2013-11-25   Apple   7.6   Green
3  2013-11-25  Celery  10.2   Green
4  2013-11-25       X  22.1     Red
5  2013-11-25       Y   8.6  Orange

我尝试根据Fruit列找出这两个 Dataframe 之间的差异
这就是我现在正在做的事情,但我没有得到预期的输出

mapped_df = pd.concat([df1,df2],ignore_index=True).drop_duplicates(keep=False)
print(mapped_df)

预期产出

Date Fruit   Num   Color
8  2013-11-25     X  22.1     Red
9  2013-11-25     Y   8.6  Orange
tjrkku2a

tjrkku2a1#

您可以使用求反的isin

output = df2.loc[~df2['Fruit'].isin(df1['Fruit'])]

相关问题