3

I have multiple datasets with different number of rows and same number of columns. I would like to find Nan values in each column for example consider these two datasets:

dataset1 :            dataset2:
a  b                  a    b
1  10                 2    11
2  9                  3    12
3  8                  4    13
4  nan                nan  14
5  nan                nan  15
6  nan                nan  16

I want to find nan values in two datasets a and b : if it occurs in column b then remove all the rows that have nan values. and if it occurs in column a then fill that values with 0.

this is my snippet code:

a=pd.notnull(data['a'].values.any())
b= pd.notnull((data['b'].values.any()))
if a:
     data = data.dropna(subset=['a'])
if b:
     data[['a']] = data[['a']].fillna(value=0)

which does not work properly.

1
  • FWIW: its recommended to use pd.notna instead of notnull because notnull doesn't capture all variations of nan Commented Jun 17, 2021 at 20:35

2 Answers 2

4

You just need fillna and dropna without control flow

data = data.dropna(subset=['b']).fillna(0)
Sign up to request clarification or add additional context in comments.

Comments

2

Pass your condition to a dict

df=df.fillna({'a':0,'b':np.nan}).dropna()

You do not need 'b' here

df=df.fillna({'a':0}).dropna()

EDIT :

df.fillna({'a':0}).dropna()
Out[1319]: 
     a   b
0  2.0  11
1  3.0  12
2  4.0  13
3  0.0  14
4  0.0  15
5  0.0  16

6 Comments

Still have nan values in column b
@AlterNative you can only choose one as the accepted answer (-:
You don't need the 'b' key at all. df.fillna({'a': 0}).dropna()
it does not work on my second dataset. It removes all the rows, I need to keep them and set the values for the rows in column a to 0.
@AlterNative have nice day :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.