'''
perstate = df[df['State'] != '']['State'].value_counts().to_dict()
data = [dict(
type = 'choropleth',
autocolorscale = False,
colorscale = 'Blues',
reversescale = True,
locations = list(perstate.keys()),
locationmode = 'USA-states',
text = list(perstate.values()),
z = list(perstate.values()),
marker = dict(
line = dict(
color = 'rgb(255, 255, 255)',
width = 2)
),
)]
layout = dict(
title = 'Bachelor contestants by State',
geo = dict(
scope = 'usa',
projection = dict(type = 'albers usa'),
countrycolor = 'rgb(255, 255, 255)',
showlakes = True,
lakecolor = 'rgb(255, 255, 255)')
)
figure = dict(data = data, layout = layout)
iplot(figure)
'''
这给了我以下内容:TypeError追溯(最近一次通话结束)
<ipython-input-294-8cdc028c4fa9> in <module>
7 colorscale = 'Blues',
8 reversescale = True,
----> 9 locations = list(perstate.keys()),
10 locationmode = 'USA-states',
11 text = list(perstate.values()),
TypeError: 'list' object is not callable
今天早些时候工作了,我什么都没改变。为什么会这样呢?我在Kaggle使用笔记本电脑。我正在尝试使用“州”列,其中包含州的缩写,我想制作一个热图。
基于错误,很可能您已将名称list
重新定义为变量名称,因此,当您调用list()
将变量转换为类型为list
时,Python解释器实际上是尝试将重新定义的变量作为函数调用。
示例:
print(list("123")) # prints ["1", "2", "3"]
现在,如果您定义了一个名为list
的变量,则会发生这种情况:
list = ["redefining", "list"]
print(list("123")) # TypeError: 'list' object is not callable
Python认为您已尝试将变量作为函数调用!
要解决此问题,只需将list
重命名为另一个名称。
perstate.keys()
-> perstate.keys
这是我的最佳猜测。