簡體   English   中英

我應該如何處理python中的不可下標問題?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM