我有两个数据框,如:df: a b c d0 12 "vik" [9, 18] "SS"1 13 "Rah" [10, 18] "YY"df2: a b c d0 12 "vik" [9, 18] "SS"1 13 "Rah" [10, 18] "YY"2 14 "Dil" [11, 18] "ZZ"我想从 df2 中消除 df 中的行。我试过了df2.sub(df, fill_values=0)这给了我一个错误TypeError: unsupported operand type(s) for -: 'str' and 'str'。我想要的输出是这样的: a b c d0 14 "Dil" [11, 18] "ZZ"任何帮助都是可观的。
查看完整描述
TA贡献1780条经验 获得超1个赞
使用merge与左连接和参数indicator=True,然后通过过滤query和删除列_merge:
df1['c'] = df1['c'].apply(tuple)
df2['c'] = df2['c'].apply(tuple)
df3 = (df2.merge(df, how='left', indicator=True)
.query('_merge == "left_only"')
.drop('_merge', axis=1))
df3['c'] = df3['c'].apply(list)
print (df3)
a b c d
2 14 Dil [11, 18] ZZ
查看完整回答