'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
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 1:[1]
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
Solution 2:[2]
Assuming t1 and t2 Series, I would use a merge:
t1.to_frame().merge(t2.reset_index(), how='left')['index']
output:
0 NaN
1 3.0
2 NaN
Name: index, dtype: float64
From the dataframes df1 and df2 and using "col" as column name:
df1.merge(df2.reset_index(), on='col', how='left')['index']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | mozway |
