Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13861

Finding the index of values of one dataframe in another

$
0
0

I know this way for finding an index of a value in a dataframe like 1 in column a and 2 in column b:

index = df.index[(df['a'] == 1) & (df['b'] == 2)]

But if I want to find the index of the values of two columns of a dataframe in another dataframe, what is the way?

For example, for two dataframes like df1 and df2 with a and b columns I have tried this:

index = [i for i in df2.index if len(df1.index[(df1['a'] == df2.loc[i,'a']) & (df1['b'] == df2.loc[i,'b'])]) == 1]

It works and returns a list, but I want to find a faster way that its result will be like first code that i mentioned.


for dear gtomer:

df1 = a         bbook      1book      2pen       1eraser    3book      4df2 = a         bpen       4book      2pen       8eraser    3book      14

I want two find Intersection between df1 and df2


Viewing all articles
Browse latest Browse all 13861

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>