【发布时间】:2021-09-24 20:51:54
【问题描述】:
尝试使用 np.select 或 np.where 在 DataFrame 列上运行方法。无论哪种方式,我都会收到错误消息,提示“对象没有属性'select'(如果我使用np.where,则为'where')。当我转换列并检查特定的列类型时,它会以numpy形式返回.ndarray。根据 numpy 文档,np.where 和 np.select 都应该适用于 numpy 数组。
在我转换为 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'
【问题讨论】:
-
你为什么使用
np.array(x).select?如果您认为这是正确的,请用文档证明它的合理性! -
select是nympy中的function,而不是method数组中的method。
标签: python numpy numpy-ndarray attributeerror