Home > Enterprise >  python notnull filter - nan values
python notnull filter - nan values

Time:05-26

I have a simplified dataframe which looks something like this:

Time    Type    Val
03:55   A       1
04:00   B       2
04:05   B       2
04:10   B       NaN
04:15   A       7.2
04:20   A       9.6
04:25   B       NaN
04:30   A       NaN
...

I want to filter out the NaNs of group B without filtering out the NaNs of group A, expected output:

Time    Type    Val
03:55   A       1
04:00   B       2
04:05   B       2
04:15   A       7.2
04:20   A       9.6
04:30   A       NaN
...

I've tried df[df.Val.notnull()] but this removes all the nulls including the ones from group A.

CodePudding user response:

Cain another condition with | for bitwise OR for test if not equal B:

df = df[df.Val.notna() | df.Type.ne('B')]
print (df)
    Time Type  Val
0  03:55    A  1.0
1  04:00    B  2.0
2  04:05    B  2.0
4  04:15    A  7.2
5  04:20    A  9.6
7  04:30    A  NaN
  • Related