3 回答

TA贡献1820条经验 获得超10个赞
使用Series.isin
+Series.duplicated
创建一个布尔掩码并使用此掩码过滤来自以下行的行df1
:
m = df1['a'].isin(df2['a']) & ~df1['a'].duplicated()
df = df1[~m]
结果:
print(df)
a b c
0 1 1 6
1 1 2 5
3 3 4 3
5 4 6 1

TA贡献1829条经验 获得超7个赞
尝试这个
df1=pd.DataFrame({'a':[1,1,2,3,4,4],'b':[1,2,3,4,5,6],'c':[6,5,4,3,2,1]})
df2=pd.DataFrame({'a':[2,4,2],'b':[1,2,3],'c':[6,5,4]})
for x in df2.a:
if x in df1.a:
df1.drop(df1[df1.a==x].index[0], inplace=True)
print(df1)

TA贡献1775条经验 获得超11个赞
尝试这个:
import pandas as pd
df1=pd.DataFrame({'a':[1,1,2,3,4,4],'b':[1,2,3,4,5,6],'c':[6,5,4,3,2,1]})
df2=pd.DataFrame({'a':[2,4,2],'b':[1,2,3],'c':[6,5,4]})
df2a = df2['a'].tolist()
def remove_df2_dup(x):
if x in df2a:
df2a.remove(x)
return False
return True
df1[df1.a.apply(remove_df2_dup)]
它从 中创建一个列表df2['a'],然后根据 中的每个值检查该列表df1['a'],每次在中匹配时从列表中删除值df1
添加回答
举报