本記事では、GoogleColab上で、データサイエンス(分析, 可視化)に取り組む際の参照コードを集めています。
GitHub
- Publicリポジトリの場合
!git clone [gitのHTTPS]
- Privateリポジトリの場合
参考LINK:
%env TOKEN=[アクセストークン] !git clone https://$$TOKEN@github.com/[アカウント名]/[リポジトリ名].git
https://docs.github.com/ja/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
DataFrame×JSONL
- JSONLファイルをDataFrameへ読み込む
df = pd.read_json('readfile.jsonl', orient='records', lines=True) df.head()
- DataFrameをJSONLファイルへ書き込む
df.to_json('writefile.jsonl', orient='records', lines=True, force_ascii=False)
DataFrame×GoogleSpreadseets
- GoogleSpreadseetsをDataFrameへ読み込む
#spreadsheets認証 from google.colab import auth auth.authenticate_user() import gspread from google.auth import default creds, _ = default() gc = gspread.authorize(creds) #spreadsheets-URL url = "[URL]" ss = gc.open_by_url(url) #spreadsheet指定 st = ss.get_worksheet(0) #データフレーム化 import pandas as pd df = pd.DataFrame(st.get_all_values()) df.columns = list(df.loc[0, :]) df.drop(0, inplace=True) df.reset_index(inplace=True) df.drop('index', axis=1, inplace=True) df.head()
Download
!zip -r dirname.zip dirname
from google.colab import files
files.download('dirname.zip')