Pandas Lambda Function with Nan Support

前端 未结 4 1023
南旧
南旧 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:03

    Within pandas 0.24.2, I use

    df.apply(lambda x: x['col_name'] if x[col1] is np.nan else expressions_another, axis=1)
    

    because pd.isnull() doesn't work.

    in my work,I found the following phenomenon,

    No running results:

    df['prop'] = df.apply(lambda x: (x['buynumpday'] / x['cnumpday']) if pd.isnull(x['cnumpday']) else np.nan, axis=1)
    

    Results exist:

    df['prop'] = df.apply(lambda x: (x['buynumpday'] / x['cnumpday']) if x['cnumpday'] is not np.nan else np.nan, axis=1)
    

提交回复
热议问题