[英]AttributeError: 'function' object has no attribute 'sum' pandas
I have the following data frame in Pandas... 我在Pandas中有以下数据框...
+-----------------------+
| | count |
+-----------------------+
| group | |
+-----------------------+
| 11- | 99435 |
+-----------------------+
| Bachelor+ | 64900 |
+-----------------------+
| Just 12 | 162483 |
+-----------------------+
| Some College | 61782 |
+-----------------------+
I want to perform the following code but I'm getting an error... 我想执行以下代码但是我收到错误...
death_2013['percent_of_total'] = death_2013.count.apply(
lambda x: (x / death_2013.count.sum()))
I'm getting the following error... 我收到以下错误...
AttributeError: 'function' object has no attribute 'apply'
I checked the death_2013.dtypes
and count
is a int64. 我检查了death_2013.dtypes
并且count
是一个int64。 I can't figure out what is wrong with the code. 我无法弄清楚代码有什么问题。
There is a pandas.DataFrame.count
method, which is shadowing the name of your column. 有一个pandas.DataFrame.count
方法,它隐藏了列的名称。 This is why you're getting this error message - the bound method count
is being accessed, which then obviously doesn't work. 这就是您收到此错误消息的原因 - 正在访问绑定的方法count
,这显然不起作用。
In this case, you should simply use the ['name_of_column']
syntax to access the count
column in both places, and be mindful of DataFrame method names when naming columns in the future. 在这种情况下,您只需使用['name_of_column']
语法访问两个位置的count
列,并在将来命名列时注意DataFrame方法名称。
death_2013['percent_of_total'] = death_2013['count'].apply(
lambda x: (x / death_2013['count'].sum()))
Note however that in this particular case there is no need to use apply
- you can simply divide the entire Series by the mean. 但请注意,在这种特殊情况下,不需要使用apply
- 您可以简单地将整个系列除以平均值。
death_2013['count'] / death_2013['count'].sum()
The problem is that dataframes have a count
method. 问题是数据帧有一个count
方法。 If you want to run apply()
on a columns named count
use the syntax 如果要对名为count
的列运行apply()
,请使用语法
death_2013['count'].apply()
Alternatively, rename the column. 或者,重命名该列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.