AttributeError: the 'function' object does not have the 'sum' pandas attribute

I have the following data frame in Pandas ...

+-----------------------+
|              | count  |
+-----------------------+
| group        |        |
+-----------------------+
| 11-          | 99435  |
+-----------------------+
| Bachelor+    | 64900  |
+-----------------------+
| Just 12      | 162483 |
+-----------------------+
| Some College | 61782  |
+-----------------------+

I want to execute the following code, but I get an error ...

death_2013['percent_of_total'] = death_2013.count.apply(
     lambda x: (x / death_2013.count.sum()))

I get the following error ...

AttributeError: 'function' object has no attribute 'apply'

I checked death_2013.dtypesand count- this is int64. I can not understand what is wrong with the code.

+4
source share
2 answers

There is a method pandas.DataFrame.countthat obscures the name of your column. That's why you get this error message - the bound method is invoked count, which obviously does not work.

['name_of_column'] count ​​ DataFrame .

death_2013['percent_of_total'] = death_2013['count'].apply(
    lambda x: (x / death_2013['count'].sum()))
+2

, dataframes count. apply() count,

death_2013['count'].apply()

.

+1

Source: https://habr.com/ru/post/1671478/


All Articles