Home > Back-end >  How should I filter one dataframe by entries from another one in pandas with isin?
How should I filter one dataframe by entries from another one in pandas with isin?

Time:01-03

I have two dataframes (df1, df2). The columns names and indices are the same (the difference in columns entries). Also, df2 has only 20 entries (which also existed in df1 as i said). I want to filter df1 by df2 entries, but when i try to do it with isin but nothing happens.

df1.isin(df2) or df1.index.isin(df2.index)

Tell me please what I'm doing wrong and how should I do it..

CodePudding user response:

First of all the isin function in pandas returns a Dataframe of booleans and not the result you want. So it makes sense that the cmds you used did not work.

I am possitive that hte following psot will help

pandas - filter dataframe by another dataframe by row elements

CodePudding user response:

If you want to select the entries in df1 with an index that is also present in df2, you should be able to do it with:

df1.loc[df2.index]

or:

df1[df1.index.isin(df2.index)]
  • Related