简体   繁体   中英

Python Pandas isin function

I have two Pandas series

s1=pd.Series(np.round(np.linspace(-1.5, 4, 55001), 4))
s2=pd.Series(np.round(np.random.uniform(-1.5, 4, 500), 4))

I wish to find out at what indices in s1, s2 occurs. I am using isin function. However, when I am checking for length I am getting following results

>>>d=s1.isin(s2)
>>>len(np.where(d)[0])
   499
>>>d=s2.isin(s1)
>>>len(np.where(d)[0])
   500

Technically The answers for both the results should be same but I am getting different lengths. Could anyone please let know about this issue.

First we will specify a random seed

np.random.seed(0)

Your issue, is linked with duplicated values in s2, to check it just do:

print(s2.drop_duplicates().shape)
print(s2.shape)

The isin function, is like an inner join.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM