我在 Pandas 中有以下数据框...
+-----------------------+
| | count |
+-----------------------+
| group | |
+-----------------------+
| 11- | 99435 |
+-----------------------+
| Bachelor+ | 64900 |
+-----------------------+
| Just 12 | 162483 |
+-----------------------+
| Some College | 61782 |
+-----------------------+
我想执行以下代码,但出现错误...
death_2013['percent_of_total'] = death_2013.count.apply(
lambda x: (x / death_2013.count.sum()))
我收到以下错误...
AttributeError: 'function' object has no attribute 'apply'
我检查了 death_2013.dtypes
并且 count
是一个 int64。我无法弄清楚代码有什么问题。
最佳答案
有一个pandas.DataFrame.count
方法,它隐藏了您的列的名称。这就是您收到此错误消息的原因 - 正在访问绑定(bind)方法 count
,这显然不起作用。
在这种情况下,您应该简单地使用 ['name_of_column']
语法来访问两个地方的 count
列,并在命名时注意 DataFrame 方法名称 future 的专栏。
death_2013['percent_of_total'] = death_2013<b>['count']</b>.apply(
lambda x: (x / death_2013<b>['count']</b>.sum()))
但请注意,在这种特殊情况下,无需使用 apply
- 您可以简单地将整个系列除以平均值。
death_2013['count'] / death_2013['count'].sum()
关于python - 属性错误 : 'function' object has no attribute 'sum' pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42602399/