ImMJ
ImMJ

Reputation: 206

How to keep True and None Value using pandas?

I've one DataFrame

import pandas as pd
data = {'a': [1,2,3,None,4,None,2,4,5,None],'b':[6,6,6,'NaN',4,'NaN',11,11,11,'NaN']}
df = pd.DataFrame(data)
condition = (df['a']>2) | (df['a'] == None)
print(df[condition])

     a    b
  0  1.0    6
  1  2.0    6
  2  3.0    6
  3  NaN  NaN
  4  4.0    4
  5  NaN  NaN
  6  2.0   11
  7  4.0   11
  8  5.0   11
  9  NaN  NaN

Here, i've to keep where condition is coming True and Where None is there i want to keep those rows as well.

Expected output is :

      a    b
  2  3.0    6
  3  NaN  NaN
  4  4.0    4
  5  NaN  NaN
  7  4.0   11
  8  5.0   11
  9  NaN  NaN

Thanks in Advance

Upvotes: 0

Views: 676

Answers (1)

anky
anky

Reputation: 75100

You can use another | or condition (Note: See @ALlolz's comment, you shouldnt compare a series with np.nan)

condition = (df['a']>2) | (df['a'].isna())
df[condition]

     a    b
2  3.0    6
3  NaN  NaN
4  4.0    4
5  NaN  NaN
7  4.0   11
8  5.0   11
9  NaN  NaN

Upvotes: 2

Related Questions