pandas get rows which are NOT in other dataframe

后端 未结 13 847
春和景丽
春和景丽 2020-11-22 02:17

I\'ve two pandas data frames which have some rows in common.

Suppose dataframe2 is a subset of dataframe1.

How can I get the rows of dataframe1 which

13条回答
  •  孤独总比滥情好
    2020-11-22 02:48

    My way of doing this involves adding a new column that is unique to one dataframe and using this to choose whether to keep an entry

    df2[col3] = 1
    df1 = pd.merge(df_1, df_2, on=['field_x', 'field_y'], how = 'outer')
    df1['Empt'].fillna(0, inplace=True)
    

    This makes it so every entry in df1 has a code - 0 if it is unique to df1, 1 if it is in both dataFrames. You then use this to restrict to what you want

    answer = nonuni[nonuni['Empt'] == 0]
    

提交回复
热议问题