Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting .eq to ignor NAN values

I have a data frame (df) that looks like

PID     SID     RID     
124     294     294
954     299     299
NAN     949     493
959     NAN     959
059     059     059 
0405    NAN     NAN
493     942     395

I used

testdf = df.eq(df["PID"], axis='index').all(axis=1) 

to get a list (testdf) that reports if the values across roles are equal, this works except that the NAN get in the way.

I tried to use

testdf = df.eq(df["PID"], axis='index').all(axis=1).notnull()

but for some reason it reports everything as equal when i know some rows aren't.

Here is an example of what I would want testdf to look like in the end

0    False
1    False
2    False
3    True
4    True
5    False
6    False
like image 585
r3vdev Avatar asked Oct 18 '25 10:10

r3vdev


1 Answers

If NaN can be ignored we can fill NaN in each column (RID and SID) with each other values. If the remaining values are equal to the PID the result will be True else it will be False. You can do it on a copy of the DataFrame in order to not alter your original data.

df['SID'] = df['SID'].fillna(df['RID'])
df['RID'] = df['RID'].fillna(df['SID'])
testdf = df.eq(df['PID'], axis='index').all(axis=1)
testdf

Here is the result:

0    False
1    False
2    False
3     True
4     True
5    False
6    False
like image 172
Romain Avatar answered Oct 20 '25 01:10

Romain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!