我有两个pandas
数据框:
df1.ix[1:5]
Keys ColName
1 LOSTP LOSTP1
2 LOSTP LOSTP2
3 LOSTP LOSTP3
4 GIDEO GIDEOasdun
5 sdfff sdfffvrf
df2.ix[1:5]
Keys ColName
2 LOSTQ LOSTQ2
3 LOSTR LOSTR3
5 sdfff sdfffvrf
我想从df1
中提取以下内容:
Keys ColName
1 LOSTP LOSTP1
2 LOSTP LOSTP2
3 LOSTP LOSTP3
4 GIDEO GIDEOasdun
暗示df1['keys] difference df2['keys']
。即,通过在keys
列
答案 0 :(得分:0)
#use an apply function to see if the colname in the current row is in df2 colnames with the same key as the current row. Then use this mask array to select rows.
df1[df1.apply(lambda x: x.ColName not in df2[df2.Keys==x.Keys]['ColName'].tolist(), axis=1)]
Out[272]:
Keys ColName
1 LOSTP LOSTP1
2 LOSTP LOSTP2
3 LOSTP LOSTP3
4 GIDEO GIDEOasdun