python - future 警告 : elementwise comparison failed; returning scalar instead

标签 python pandas

我收到警告,我想检查这是否会中断。我在很多情况下都像这样使用 np.where(对我来说,它类似于 excel 中的 if 语句)。有没有更好或更多的pythonic或pandas方法来做到这一点?我试图将一维变成我可以轻松进行数学运算的东西。

df['closed_item'] = np.where(df['result']=='Action Taken', 1, 0)

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  result = getattr(x, name)(y)


INSTALLED VERSIONS
------------------
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 10

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.0
setuptools: 20.2.2
Cython: 0.23.4
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.0.0
sphinx: 1.3.1
patsy: 0.4.0
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.1
matplotlib: 1.5.1
openpyxl: 2.2.6
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.7.7
lxml: 3.4.4
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.38.0

最佳答案

比较数据集中的“int”和“str”时会出现此警告。将 .astype(int) 添加到您的比较数据集。
尝试:

df['closed_item'] = np.where(df['result'].astype(str)=='Action Taken', 1, 0)

关于python - future 警告 : elementwise comparison failed; returning scalar instead,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36620175/

相关文章:

Python tkinter : How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

python - python字典的编码指南

Python:如何将 ggplot 与简单的 2 列数组一起使用?

python - 根据另一列的值将列添加到 Pandas 数据框

python - KeyError 如果 is_scalar(key) 和 isna(key) 而不是 self.hasnans :

python - 从 __init__.py 中导入类以生成更短的导入语句是否存在问题?

python - Pandas 数据框中按键列分组的连续空值的计数

python - 导入不规则大小的文本文件

python-3.x - 如何 'exactly' 在一系列日期字符串上使用 .to_datetime?

python - 如何让这个输出在 Pandas 中按日期排序