Python Pandas:“numpy.ndarray”对象没有属性“apply”

Posted

技术标签:

【中文标题】Python Pandas:“numpy.ndarray”对象没有属性“apply”【英文标题】:Python Pandas: 'numpy.ndarray' object has no attribute 'apply' 【发布时间】:2017-03-06 14:50:42 【问题描述】:

我有一个数据框,我从中选择了唯一值,它产生了一个形状为 (1187,) 的 ndarray(“unManager”)......只有一列。

现在,我编写了一个函数来对数据框的一些行进行分组,进行计算并在 ndarray 中添加值。

为此,我在 ndarray ("unManager") 上使用了 apply,并且收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-48-ff7e78ab33a7> in <module>()
----> 1 unManager.apply(runThis, axis=0)

AttributeError: 'numpy.ndarray' object has no attribute 'apply'

现在,当我尝试将 ndarray(“unManager”)转换为数据框时,通过:

dfs = pd.DataFrame(unManager,index=unManager[0])

我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-3ee3d2605321> in <module>()
----> 1 dfs = pd.DataFrame(unManager,index=unManager[0])
.
.
TypeError: Index(...) must be called with a collection of some kind, 'actama99,CLE' was passed

'actama99,CLE' 这里是形状 (1187,) 的 ndarray ("unManager") 的第一个值。

谁能告诉我我做错了什么? TIA

【问题讨论】:

在这种情况下您应该使用 applymap。另外,对于第二个问题,你可以试试下面的dfs = pd.DataFrame(unManager).reset_index(drop=True) 【参考方案1】:
dfs = pd.DataFrame(unManager,index=unManager[0])

unManager[0] 返回一个标量,它不是collection

你想要的

dfs = pd.DataFrame(dict(unManagers=unManager))

【讨论】:

谢谢。现在,当我运行应用函数时... dfs.apply(func, axis=0) 我收到错误:ValueError: ('Series lengths must match to compare', 'occured at index unManagers')

以上是关于Python Pandas:“numpy.ndarray”对象没有属性“apply”的主要内容,如果未能解决你的问题,请参考以下文章

如何绘制 K-mean 算法的混淆/相似矩阵

python pandas怎么用

python(pandas模块)?

python(pandas模块)?

python怎么打开pandas

Python pandas用法