[英]np.where and np.select returning error. AttributeError: 'numpy.ndarray' object has no attribute 'select'
Trying to use np.select or np.where to run a method on a DataFrame Column.尝试使用 np.select 或 np.where 在 DataFrame 列上运行方法。 Either way I do it, I get the error saying the "object has no attribute 'select' (or 'where' if I use np.where). When I convert the column and check the specific column type, it returns as an numpy.ndarray. According to the numpy docs, both np.where and np.select should work on numpy arrays.无论采用哪种方式,我都会收到错误消息,指出“对象没有属性‘select’(或‘where’,如果我使用 np.where)。当我转换列并检查特定的列类型时,它返回为一个 numpy .ndarray. 根据 numpy 文档, np.where和np.select都应该在 numpy 数组上工作。
Checking type on the Series I converted to a numpy array, after checking the type I try the method and receive the errors:检查系列上的类型我转换为 numpy 数组,在检查类型后我尝试该方法并收到错误:
type(np.array(daily_std_df['Daily_Std']))
numpy.ndarray
type(np.array(x))
numpy.ndarray
### Apply method to the data frame using np.where OR np.select:
x = daily_std_df['Daily_Std']
conditionList = [x > SP_500_std, x < SP_500_std]
choiceList = ['High', "Not High"]
daily_std_df["Risk"] = np.array(x).select(conditionList, choiceList)
daily_std_df
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-51-42673f1eeafa> in <module>
3 conditionList = [x > SP_500_std, x < SP_500_std]
4 choiceList = ['High', "Not High"]
----> 5 daily_std_df["Risk"] = np.array(x).select(conditionList, choiceList)
6 daily_std_df
AttributeError: 'numpy.ndarray' object has no attribute 'select'
It is not np.array
which has the select
attribute, it's just simply np
that has the attribute.这不是np.array
它具有select
属性,它只是简单的np
具有属性。
So instead of:所以而不是:
daily_std_df["Risk"] = np.array(x).select(conditionList, choiceList)
Try this:试试这个:
daily_std_df["Risk"] = np.select(conditionList, choiceList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.