Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using similar method like .isin but with row number returned

I want to get the row number of matching condition similar like .isin, part of dataframe are shown like this

df1

0    452525
1    110101
2    393910

df2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

0     100000
1     110000
2     110100
3     110101
4     110102
5     110105
6     110106
7     110107
8     110108
9     110109
10    110111
11    110112
12    110113
13    110114
14    110115

when I use

t1.isin(t2)

I got

0    False
1     True
2    False

My desired output is

0    NaN
1    3
2    NaN

Is there a method to do this? But I want to avoid using pd.merge() since both dataframe is big, if merging them would take long time to execute

>Solution :

Use Series.map by dictionary with swap values and index of t2:

#if t1, t2 are Series
out = t1.map(dict(zip(t2, t2.index)))

#if t1, t2 are Dataframes
out = t1['col'].map(dict(zip(t2['col'], t2.index)))
print (out)
0    NaN
1    3.0
2    NaN
Name: col, dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading