![](/img/trans.png)
[英]TypeError: 'int' object is not subscriptable. What Should I do?
[英]What I should to do with not subscriptable problem in python?
我的想法是准備一張美國地圖,其中包含“出生”最多的 NBA 球員的信息。
我發現這個教程https://plot.ly/python/choropleth-maps/
我試圖修改這樣的代碼:
import plotly.plotly as py
import pandas as pd
df = pd.ExcelFile('playersnba.xlsx')
data = [ dict(
type = 'choropleth',
locations = df['State'],
z = df['Numbers'],
text = df['State'],
colorscale = [[0,"rgb(5, 10, 172)"],[0.35,"rgb(40, 60, 190)"],[0.5,"rgb(70, 100, 245)"],\
[0.6,"rgb(90, 120, 245)"],[0.7,"rgb(106, 137, 247)"],[1,"rgb(220, 220, 220)"]],
autocolorscale = False,
reversescale = True,
marker = dict(
line = dict (
color = 'rgb(180,180,180)',
width = 0.5
) ),
colorbar = dict(
autotick = False,
tickprefix = '#',
title = 'players'),
) ]
我的問題是:
...line 8, in <module>
locations = df['State'],
TypeError: 'ExcelFile' object is not subscriptable
應該這樣寫
import pandas as pd
df = pd.ExcelFile('playersnba.xlsx').parse()
將 excel 文件讀入數據框的方法是使用ExcelFile.parse()
,而不僅僅是 ExcelFile。 所以:
df = pd.ExcelFile.parse('playersnba.xlsx')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.