Skip to content

Commit 2bda5bd

Browse files
committed
DOC: Add Google Colab data loading guide
Adds comprehensive guide for loading data in Google Colab environment. Covers Google Drive, local uploads, URLs, and Google Sheets with working code examples for each method. Closes #62708
1 parent 9f66b81 commit 2bda5bd

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

doc/source/user_guide/io.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6316,6 +6316,91 @@ More information about the SAV and ZSAV file formats is available here_.
63166316

63176317
.. _here: https://www.ibm.com/docs/en/spss-statistics/22.0.0
63186318

6319+
6320+
.. _io.colab:
6321+
6322+
Loading Data in Google Colab
6323+
-----------------------------
6324+
6325+
Google Colab is a popular cloud-based Jupyter notebook environment. pandas works seamlessly in Colab, and there are several ways to load data:
6326+
6327+
From Google Drive
6328+
~~~~~~~~~~~~~~~~~
6329+
6330+
To access files stored in your Google Drive:
6331+
6332+
.. code-block:: python
6333+
6334+
from google.colab import drive
6335+
import pandas as pd
6336+
6337+
# Mount your Google Drive
6338+
drive.mount('/content/drive')
6339+
6340+
# Read file from Drive
6341+
df = pd.read_csv('/content/drive/MyDrive/your_file.csv')
6342+
6343+
From Local Computer
6344+
~~~~~~~~~~~~~~~~~~~
6345+
6346+
To upload files from your local machine:
6347+
6348+
.. code-block:: python
6349+
6350+
from google.colab import files
6351+
import pandas as pd
6352+
import io
6353+
6354+
# Upload file (opens file picker dialog)
6355+
uploaded = files.upload()
6356+
6357+
# Read the uploaded file
6358+
for filename in uploaded.keys():
6359+
df = pd.read_csv(io.BytesIO(uploaded[filename]))
6360+
6361+
From URL
6362+
~~~~~~~~
6363+
6364+
Direct URL loading works the same as in standard pandas:
6365+
6366+
.. code-block:: python
6367+
6368+
import pandas as pd
6369+
6370+
# Read from any public URL
6371+
url = 'https://raw.githubusercontent.com/example/repo/main/data.csv'
6372+
df = pd.read_csv(url)
6373+
6374+
From Google Sheets
6375+
~~~~~~~~~~~~~~~~~~
6376+
6377+
To read data from Google Sheets:
6378+
6379+
.. code-block:: python
6380+
6381+
import pandas as pd
6382+
6383+
# Option 1: Export as CSV (sheet must be publicly accessible)
6384+
sheet_id = 'your-spreadsheet-id'
6385+
sheet_name = 'Sheet1'
6386+
url = f'https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}'
6387+
df = pd.read_csv(url)
6388+
6389+
# Option 2: Using authentication for private sheets
6390+
from google.colab import auth
6391+
import gspread
6392+
from google.auth import default
6393+
6394+
auth.authenticate_user()
6395+
creds, _ = default()
6396+
gc = gspread.authorize(creds)
6397+
6398+
worksheet = gc.open('Your Spreadsheet Name').sheet1
6399+
data = worksheet.get_all_values()
6400+
df = pd.DataFrame(data[1:], columns=data[0])
6401+
6402+
For more details on Colab-specific I/O operations, see the `official Google Colab I/O guide <https://colab.research.google.com/notebooks/io.ipynb>`_.
6403+
63196404
.. _io.other:
63206405

63216406
Other file formats

0 commit comments

Comments
 (0)