Error: float object has no attribute notnull

前端 未结 4 921
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-18 19:15

I have a dataframe:

  a     b     c
0 nan   Y     nan
1  23   N      3
2 nan   N      2
3  44   Y     nan

I wish to have this output:



        
4条回答
  •  无人共我
    2021-02-18 19:43

    You don't need apply, use np.where:

    df['d'] = np.where(df.a.isnull(),
             np.nan,
             np.where((df.b == "N")&(~df.c.isnull()),
                      df.a*df.c,
                      df.a))
    

    Output:

          a  b    c     d
    0   NaN  Y  NaN   NaN
    1  23.0  N  3.0  69.0
    2   NaN  N  2.0   NaN
    3  44.0  Y  NaN  44.0
    

提交回复
热议问题