mardi 19 avril 2016

Python If statement on dataframe

I would like to replace column df['pred'] with 0 if the respective value of df['nonzero'] is not 'NAN' and "<= 1".

         beta0  beta1  number_repair   t  pred  nonzero  
0          NaN    NaN            NaN   6     0      NaN  
1          NaN    NaN            NaN   7     0      NaN  
2          NaN    NaN            NaN   8     0      NaN  
3          NaN    NaN            NaN   9     3      0  
4          NaN    NaN            NaN  10     2      0  
5          NaN    NaN            NaN  11     1      0  

I tried the following code but it returned error. How could I correct the code or could someone suggest other way to achieve it? Thanks!

mapping['pred'] = 0 if (np.all(np.isnan(mapping['nonzero'])),
(mapping['nonzero'] <= 1)) else mapping['pred']

Aucun commentaire:

Enregistrer un commentaire