Home > Net >  How to use lambda function just in null rows of a column in pandas
How to use lambda function just in null rows of a column in pandas

Time:10-04

I am trying to put 0 or 1 in place of the null rows of a column using lambda function, but my code doesn't make any changes in the data.

df[df['a'].isnull()]['a']=df[df['a'].isnull()].apply(lambda x:1 if (x.b==0 and x.c==0) else 
                                                      0,axis=1) 

Where I am wrong in this?? sample table

CodePudding user response:

You can use loc to specifically fill the null value rows in your DataFrame. When you're using the apply method you can use it on the entire DataFrame, you do not need to filter for NULL values there. The loc will take care of only filling the rows which meet the NULL condition. This should work :

df['a'].loc[df['a'].isnull()] = df.apply(lambda x:1 if (x.b==0 and x.c==0) else 
                                                  0,axis=1)
  • Related