简体   繁体   中英

Pandas isin for multiple columns does not seem to be matching?

I have a dataframe (eval_datan) that looks like:

    ccs1    ccs2    ccs3    ccs4    ccs5    ccsp1   ccsp2   ccsp3   ccsp4   ccsp5   
0   101     164     53      98      200     102     100     256     259     133 
1   204     120     147     258     151     47      256     259     48 
49  
2   197     2       39      253     259     259     49      98      256     257 
3   198     258     126     127     128     133     128     256     134     92  
4   204     211     232     244     95      259     256     257     98  
254 

And I'm trying to determine if the ccs5 is in any of ccsp1-ccsp5

my code is:

eval_datan['correct']=(eval_datan['ccs5'].isin (eval_datan[['ccsp1','ccsp2','ccsp3','ccsp4','ccsp5']]))

I'm getting all false Row 2 should say true as ccs5=259 as does ccsp1 at 1st I thought it could be differing number types but I recast the ccsp columns as uint16 from int64 as numpy had originally generated that did not help. The ccs5 column is uint16.

eval_datan['correct'] = eval_datan[['ccsp1','ccsp2','ccsp3','ccsp4','ccsp5']].isin(eval_datan['ccs5']).any(axis=1)

Output:

   ccs1  ccs2  ccs3  ccs4  ccs5  ccsp1  ccsp2  ccsp3  ccsp4  ccsp5  correct
0   101   164    53    98   200    102    100    256    259    133    False
1   204   120   147   258   151     47    256    259     48     49    False
2   197     2    39   253   259    259     49     98    256    257     True
3   198   258   126   127   128    133    128    256    134     92     True
4   204   211   232   244    95    259    256    257     98    254    False

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