Q:

pandas.DataFrame.apply() produces NaN when filtering with square brackets

import pandas as pd
df = pd. DataFrame({"First_Column": [-2,-1,1,2,3]})
df['Second_Column']='Good'
df.loc[:, 'Second_Column']=df[df. First_Column>0]['Second_Column'].apply(lambda x: 'Bad')

When I run this I get Bad and NaN in the Second_Column, instead of Good and Bad.  Why does apply() overwrite the values that didn't meet the criteria with NaN? 

A:

By using mask
df. Second_Column=df. Second_Column.mask(df. First_Column>0,'Bad')
df
Out[441]: 
   First_Column Second_Column
0            -2          Good
1            -1          Good
2             1           Bad
3             2           Bad
4             3           Bad

Or 
df.loc[df. First_Column>0,'Second_Column']='Bad'
df
Out[443]: 
   First_Column Second_Column
0            -2          Good
1            -1          Good
2             1           Bad
3             2           Bad
4             3           Bad

Or using np.where more straightforward 
df['Second_Column']=np.where(df. First_Column>0,'Bad','Good')
df
Out[445]: 
   First_Column Second_Column
0            -2          Good
1            -1          Good
2             1           Bad
3             2           Bad
4             3           Bad

