Pandas Lambda Function with Nan Support

前端 未结 4 1014
南旧
南旧 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 19:53

    Assuming that you do have a second column, that is:

    df = pd.DataFrame({ 'Col1' : [1,2,3,np.NaN], 'Col2': [1,2,3,4]})

    The correct solution to this problem would be:

    df['Col1'].fillna(df['Col2'], inplace=True)
    

提交回复
热议问题