jeudi 5 avril 2018

Print based on one line condition when applying a function to a data frame Python

I have the following dataframe and I would like to print only the columns that have a missing Value.

data = pd.DataFrame({'col_A' : [1,2,3,4],
                   'col_B' : [1,2,np.NaN,4]});data

Funtion to
def num_missing(x): return sum(x.isnull())

while the print without the condition works:

print( data.apply(num_missing, axis=0))

Output:

col_A    0
col_B    1
dtype: int64

Desired Output:

col_B    1
dtype: int64

I tried to print with the one line if statement ... but I am doing something wrong with the syntax

print( data.apply(num_missing, axis=0) if data.apply(num_missing, axis=0)> 0)

I also tried the below if statement which is not working either:

 if data.apply(num_missing, axis=0) >0:
        print( data.apply(num_missing, axis=0) )

Could you please provide both solution and and explain me what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire