I have the following data frame in 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类型。我无法确定代码存在什么问题。
death_2013.count.apply
或者death_2013['count'].apply
,但是它们似乎都不起作用。奇怪的是,我在不同的数据集上使用这行代码时是有效的。 - redeemefydeath_2013 ['count'] .apply()
呢? - John Colemancount
是pandas或Python中的保留字。我更改了列名,现在apply()
可以正常工作了。 - redeemefy