1

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']
1
  • Do you need to use a ternary? Have you tried just looping over the rows? Commented Apr 19, 2016 at 13:03

2 Answers 2

3

I think you can use loc with mask by function notnull:

mask = (df['nonzero'].notnull()) & (df['nonzero'] <= 1)
print mask
0    False
1    False
2    False
3     True
4     True
5     True
Name: nonzero, dtype: bool

By comment (Thank you PhilChang) it is same as:

mask = df['nonzero'] <= 1
print mask
0    False
1    False
2    False
3     True
4     True
5     True
Name: nonzero, dtype: bool
df.loc[ mask, 'pred'] = 0
print df
   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     0      0.0
4    NaN    NaN            NaN  10     0      0.0
5    NaN    NaN            NaN  11     0      0.0

Another solution with mask:

df['pred'] = df.pred.mask(mask,0)
print df
   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     0      0.0
4    NaN    NaN            NaN  10     0      0.0
5    NaN    NaN            NaN  11     0      0.0
Sign up to request clarification or add additional context in comments.

1 Comment

In fact, notnull() is not necessary in this case. float number compared with nan is always false
0

I don't know how to check on a Series if cells contain 'NaN', but for the other condition, this works quite well:

df.ix[df.ix[:,'nonzero'] <=1,'pred'] = 0

You then just have to add after the first test "and my_second_test".

Comments

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.