我应该如何处理python中无法下标的问题?

时间:2018-11-19 20:59:35

标签: python pandas plotly xlsx

我的想法是准备一张美国地图,上面注明“出生”的NBA球员人数最多的信息。

My excel

我发现了本教程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

2 个答案:

答案 0 :(得分:1)

将Excel文件读入数据框的方法是使用#include <stdio.h> #include <stdlib.h> // exit(), EXIT_FAILURE #define MAX_FIRSTNAME_LEN 30 #define MAX_LASTNAME_LEN 30 #define MAX_ASSIGNMENT_LEN 10 int main( void ) { FILE *cfPtr; // cfPtr = clients.txt file pointer // fopen opens file. Exit program if unable to create file if ((cfPtr = fopen("grades.txt", "w")) == NULL) { perror( "fopen to read grades.txt failed" ); exit( EXIT_FAILURE ); } // implied else, fopen successful puts("Enter the students first & last name, ID, grade, and assignment."); puts("Enter EOF to end input."); printf( "%s", "? " ); char firstName[ MAX_FIRSTNAME_LEN ]; // student first name char lastName[ MAX_LASTNAME_LEN ]; // student last name unsigned int stuID; // student ID double grade; // student grade char assignment[ MAX_ASSIGNMENT_LEN ]; // student assignment name while( scanf( "%29s %29s %u %lf %9s", firstName, lastName, &stuID, &grade, assignment) == 5 ) { fprintf( cfPtr, "%s\t%s\t%d\t%.2f\t%s\t\n", firstName, lastName, stuID, grade, assignment); printf( "%s", "? " ); } // end while fclose( cfPtr ); // fclose closes file } // end main ,而不仅仅是ExcelFile。所以:

ExcelFile.parse()

答案 1 :(得分:1)

应该这样写

import pandas as pd

df = pd.ExcelFile('playersnba.xlsx').parse()