本文介绍了Python Pandas:"numpy.ndarray"对象没有属性"apply"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,可以从中选择唯一的值,结果得到一个形状为(1187,)的ndarray("unManager")...仅一列.

I have a dataframe from which I picked unique values, and it resulted in an ndarray ("unManager") of shape (1187,)... just one column.

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

Now, I have written a function to group some rows of the dataframe, do calculations and add values in the ndarray.

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

I am using apply on the ndarray ("unManager") for that, and am getting the following error:

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")转换为数据帧时:

Now, when I am trying to convert the ndarray ("unManager") to a dataframe, through:

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")的第一个值.

'actama99,CLE' here is first value of the ndarray ("unManager") of shape (1187,).

有人可以告诉我我做错了什么吗? TIA

Can someone please tell me what am I doing wrong? TIA

推荐答案

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

unManager[0]返回标量,而不是collection.

您想要的

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

这篇关于Python Pandas:"numpy.ndarray"对象没有属性"apply"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:58