问题:
E:\SW\Anaconda\lib\site-packages\pandas\core\ops\array_ops.py:253: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
代码:
df.loc[df['列1'] == 'None', '列1'] = 0
或
myRows = df[df['Unnamed: 5'] == 'aaa'].index.tolist()
原因:
数值类型跟字符串类型进行了比较。
具体解释:
There is a disagreement between Numpy and native python on what should happen when you compare a strings to numpy's numeric types. Notice the right operand is python's turf, a primitive string, and the middle operation is python's turf, but the left operand is numpy's turf. Should you return a Python style Scalar or a Numpy style ndarray of Boolean? Numpy says ndarray of bool, Pythonic developers disagree. Classic standoff.
Should it be elementwise comparison or Scalar if item exists in the array?
If your code or library is using the in or == operators to compare python string to numpy ndarrays, they aren't compatible, so when if you try it, it returns a scalar, but only for now. The Warning indicates that in the future this behavior might change so your code pukes all over the carpet if python/numpy decide to do adopt Numpy style.