Getting Google Spreadsheet CSV into A Pandas Dataframe

后端 未结 6 672
死守一世寂寞
死守一世寂寞 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:10

    I have been using the following utils and it worked so far:

    def load_from_gspreadsheet(sheet_name, key):
        url = 'https://docs.google.com/spreadsheets/d/{key}/gviz/tq?tqx=out:csv&sheet={sheet_name}&headers=1'.format(
            key=key, sheet_name=sheet_name.replace(' ', '%20'))
    
        log.info('Loading google spreadsheet from {}'.format(url))
    
        df = pd.read_csv(url)
        return df.drop([col for col in df.columns if col.startswith('Unnamed')], axis=1)
    

    You must specify the sheet_name and the key. The key is the string you get from the url in the following path: https://docs.google.com/spreadsheets/d/{key}/edit/.

    You can change the value of headers if you have more than one row for the column names but I am not sure if it still work with multi-headers.

    It may brake if Google will change their APIs.

    Also please bear in mind that your spreadsheet must be public, everyone with the link can read it.

提交回复
热议问题