使用if-else创建新列时出现 pandas 错误:系列的真值不明确 [英] Pandas error when using if-else to create new column: The truth value of a Series is ambiguous

查看:87
本文介绍了使用if-else创建新列时出现 pandas 错误:系列的真值不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pandas,并尝试使用Python if-else语句(又称为三元条件运算符)创建一个新列,以避免被零除.

I'm using Pandas and am trying to create a new column using a Python if-else statement (aka ternary condition operator) in order to avoid division by zero.

例如,在下面的示例中,我想通过除以A/B来创建新的列C.我想使用if-else语句来避免除以0.

For example below, I want to create a new column C by dividing A/B. I want to use the if-else statement to avoid dividing by 0.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 5, size=(100, 2)), columns=list('AB'))
df.head()
#    A  B
# 0  1  3
# 1  1  2
# 2  0  0
# 3  2  1
# 4  4  2

df['C'] = (df.A / df.B) if df.B > 0.0 else 0.0

但是,我从最后一行得到一个错误:

However, I am getting an error from the last line:

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

我在StackOverflow上进行搜索,找到了有关此错误的其他帖子,但没有一个帖子涉及这种类型的if-else语句.一些帖子包括:

I searched on StackOverflow and found other posts about this error, but none of them involved this type of if-else statement. Some posts include:

在数据框

错误:a的真实值系列不明确-Python熊猫

任何帮助将不胜感激.

推荐答案

df.B > 0产生系列,例如:

0      True  # 4 > 0 => True
1      True  # 2 > 0 => True
2      True  # ...
3      True
4      True
5      True
6      True
7      True
8     False  # 0 is not > 0 => False
9     False  # 0 is not > 0 => False
...

返回多个值会导致模棱两可(某些值为True,而其他值为False).

Multiple values are returned which results in ambiguity (some are True while others are False).

一种解决方案是使用np.where:

sentinel = np.nan  # Or 0 if you must...
df = df.assign(C=np.where(df['B'] != 0, df['A'] / df['B'], sentinel))
>>> df
   A  B    C
0  2  4  0.5
1  0  2  0.0
2  1  2  0.5
3  4  4  1.0
4  1  1  1.0
5  4  4  1.0
6  2  4  0.5
7  1  2  0.5
8  4  0  NaN  # NaN is assigned in cases where the value in Column `B` is zero.
9  1  0  NaN
...

这篇关于使用if-else创建新列时出现 pandas 错误:系列的真值不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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