在具有if条件的数据框架中使用NaN

时间:2016-07-30 18:09:17

标签: python numpy pandas

我在数据框中有2列,我正在尝试根据第二个是NaN而第一个是否有某些值来进入条件,但未成功使用:

let totallBill = 95.00
let tipPercentage: Double
let rating = 3

if rating == 5 {
    tipPercentage = 0.25
} else if rating >= 3 {
    tipPercentage = 0.15
} else {
    let foo = 0.10 // this was coincidentally called `tipPercentage`, but since this is yet another a local constant of even narrower scope, it's equivalent to using a completely different name 
}

let totalPaid = totallBill + totallBill * tipPercentage

if np.isfinite(train_bk['Product_Category_1']) and np.isnan(train_bk['Product_Category_2'])

1 个答案:

答案 0 :(得分:0)

我会使用eval:

df.eval(' ind =  ((pc1==pc1) & (pc2!=pc2) )*2+((pc1==pc1)&(pc2==pc2))*3')
df.replace({'ind':{0:1})