0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スプレッドシートをDataframeで読みたい時のスニペット

Last updated at Posted at 2023-03-22

Google Colabで、スプレッドシートのデータをDataframeとして扱いたい時によく使うスニペット

from google.colab import auth
from google.auth import default
import pandas as pd
import numpy as np
import gspread
from gspread_dataframe import get_as_dataframe, set_with_dataframe

SHEET_KEY = 'XXXXXXX';  
SHEET_NAME = 'foobar'

def main():
  #認証
  auth.authenticate_user()
  creds, _ = default()
  gc = gspread.authorize(creds)

  workbook = gc.open_by_key(SHEET_KEY)
  worksheet = workbook.worksheet(SHEET_NAME)
  df = get_as_dataframe(worksheet, usecols=range(8), header=0, evaluate_formulas=True).dropna(how='all')
  # usecols=[0,1,2,] でも良い。カラムの使用範囲をリストで渡す。

  display(df)

if __name__ == '__main__':
  main()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?