如何在熊猫中使用条件进行计算?

时间:2018-04-17 16:29:56

标签: python pandas if-statement

我正在尝试编写一个条件,我希望在新列中获得每次访问的平均成本。但是,如果访问次数为0,那么我想使用' 1'相反,Visit_cost不是无穷大。

我的做法是抛出错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

DF

id    Cost          Visit
2     52            1
3     85            0
4     853           10

df['VISIT_COST'] = [df['Cost']/df['Visit'] if df['Visit'] != 0 else df['Cost']/1]

最终df:

id    Cost          Visit    VISIT_COST
2     52            1        52
3     85            0        85
4     853           10       85.3

2 个答案:

答案 0 :(得分:3)

我认为最好是使用numpy.where,因为速度非常快:

df['VISIT_COST'] = np.where(df['Visit'] != 0, df['Cost']/df['Visit'], df['Cost'])
print (df)
   id  Cost  Visit  VISIT_COST
0   2    52      1        52.0
1   3    85      0        85.0
2   4   853     10        85.3

开箱即用解决方案 - 将0替换为1添加转换为整数的布尔掩码:

df['VISIT_COST'] = df['Cost'].div(df['Visit'].eq(0).astype(int).add(df['Visit']))
print (df)
   id  Cost  Visit  VISIT_COST
0   2    52      1        52.0
1   3    85      0        85.0
2   4   853     10        85.3

详情:

print (df['Visit'].eq(0).astype(int))
0    0
1    1
2    0
Name: Visit, dtype: int32

print (df['Visit'].eq(0).astype(int).add(df['Visit']))
0     1
1     1
2    10
Name: Visit, dtype: int64

答案 1 :(得分:2)

我认为我们应该使用mask

#df['VISIT_COST'] = np.where(df['Visit'] != 0,df['Cost']/df['Visit'] , df['Cost']/1)


df['VISIT_COST']=df.Cost.mask(df['Visit'] != 0,df['Cost']/df['Visit'])