通过比较键键来提取行

时间:2017-05-25 21:04:02

标签: python-2.7 pandas dataframe

我有两个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

上比较来找到df1中不在df2中的元素

1 个答案:

答案 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