我有两个数据帧:
df1 = {0:[1,2,3,4,5,6,7,11],1:[100,20,7]}
df2 = {0:[100,4,6,7],1:[1,3,4,7]}
我必须从df2的任何行中出现的df1中删除行
结果数据框
df3 = [2,5,11,20]
答案 0 :(得分:2)
您可以将值np.ravel
展平,并通过np.setdiff1d
得到差值:
df1 = pd.DataFrame({0:[1,2,3,4,5,6,7,11],1:[100,20,7,1,2,3,4,5]})
df2 = pd.DataFrame({0:[100,4,6,7],1:[1,3,4,7]})
L = np.setdiff1d(np.ravel(df1), np.ravel(df2)).tolist()
print (L)
[2, 5, 11, 20]
或集合差异:
L = list(set(df1.stack()) - set(df2.stack()))
print (L)
[2, 11, 20, 5]