错误:浮动对象没有属性notnull [英] Error: float object has no attribute notnull

查看:94
本文介绍了错误:浮动对象没有属性notnull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框:

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

我希望得到以下输出:

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

我希望条件为,当a列为空时,则d将为null;否则,如果b列为N且c列不为空,则d列等于a列* * c列,否则d等于a列

我已经完成了这段代码,但是出现了错误:

I have done this code but i get the error:

def f4(row):
    if row['a']==np.nan:
       return np.nan
    elif row['b']=="N" & row(row['c'].notnull()):
       return row['a']*row['c']
    else:
       return row['a']

 DF['P1']=DF.apply(f4,axis=1)

谁能帮助我指出我的错误在哪里?我已参考并尝试此操作,但也收到错误根据if-elif-else条件创建新列

can anyone help me point out where is my mistake? I have refer to this and try this but also get the error Creating a new column based on if-elif-else condition

推荐答案

您不需要apply,请使用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))

输出:

      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

这篇关于错误:浮动对象没有属性notnull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆