Home > Blockchain >  Drop indeces appearing in another dataframe
Drop indeces appearing in another dataframe

Time:12-17

I have two dataframes:

df0 = pd.DataFrame({'a':[9,8,7,6]},index=[0,1,2,4])
df1 = pd.DataFrame({'b':[5,6,4,7]},index=[2,4,6,8])

I am aiming to get the rows of df0 which have indices not appearing in df1. I perform a left-excluxing join (enter image description here

I do:

res = \
df0.merge(
    df1,how='left',left_index=True, right_index=True, indicator=True
).query('_merge=="left_only"').drop(['b','_merge'],axis=1)

Which returns the desired dataframe but seems an overkill. I thought about using filter as well:

df0.filter(items=df1.index,axis=0)

but this returns the rows from df0 which I want to drop, not the ones I want to keep.

Is there an easier way to do this than the left-excluding join shown above?

CodePudding user response:

Use Index.isin with inverted mask by ~ in boolean indexing:

res = df0[~df0.index.isin(df1.index)]
print (res)
   a
0  9
1  8
  • Related