Pandas Lambda Function with Nan Support

前端 未结 4 1007
南旧
南旧 2021-01-02 19:52

I am trying to write a lambda function in Pandas that checks to see if Col1 is a Nan and if so, uses another column\'s data. I have having trouble getting code (below) to c

4条回答
  •  轮回少年
    2021-01-02 20:06

    You need to use np.nan()

    #import numpy as np
    df2=df.apply(lambda x: 2 if np.isnan(x['Col1']) else 1, axis=1)   
    
    df2
    Out[1307]: 
    0    1
    1    1
    2    1
    3    2
    dtype: int64
    

提交回复
热议问题