Getting Google Spreadsheet CSV into A Pandas Dataframe

后端 未结 6 678
死守一世寂寞
死守一世寂寞 2020-12-04 09:31

I uploaded a file to Google spreadsheets (to make a publically accessible example IPython Notebook, with data) I was using the file in it\'s native form could be read into a

6条回答
  •  盖世英雄少女心
    2020-12-04 10:15

    You can use read_csv() on a StringIO object:

    from io import BytesIO
    
    import requests
    r = requests.get('https://docs.google.com/spreadsheet/ccc?key=0Ak1ecr7i0wotdGJmTURJRnZLYlV3M2daNTRubTdwTXc&output=csv')
    data = r.content
    
    In [10]: df = pd.read_csv(BytesIO(data), index_col=0,parse_dates=['Quradate'])
    
    In [11]: df.head()
    Out[11]: 
              City                                            region     Res_Comm  \
    0       Dothan  South_Central-Montgomery-Auburn-Wiregrass-Dothan  Residential   
    10       Foley                              South_Mobile-Baldwin  Residential   
    12  Birmingham      North_Central-Birmingham-Tuscaloosa-Anniston   Commercial   
    38       Brent      North_Central-Birmingham-Tuscaloosa-Anniston  Residential   
    44      Athens                 North_Huntsville-Decatur-Florence  Residential   
    
              mkt_type            Quradate  National_exp  Alabama_exp  Sales_exp  \
    0            Rural 2010-01-15 00:00:00             2            2          3   
    10  Suburban_Urban 2010-01-15 00:00:00             4            4          4   
    12  Suburban_Urban 2010-01-15 00:00:00             2            2          3   
    38           Rural 2010-01-15 00:00:00             3            3          3   
    44  Suburban_Urban 2010-01-15 00:00:00             4            5          4   
    
        Inventory_exp  Price_exp  Credit_exp  
    0               2          3           3  
    10              4          4           3  
    12              2          2           3  
    38              3          3           2  
    44              4          4           4  
    

提交回复
热议问题