Skip to content

Commit 26da125

Browse files
DOC: Add 'Loading data in Google Colab' section to IO user guide
1 parent 944c527 commit 26da125

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

doc/source/user_guide/io.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,71 @@ The pandas I/O API is a set of top level ``reader`` functions accessed like
4848
CSV & text files
4949
----------------
5050

51+
.. _io-colab:
52+
53+
Loading data in Google Colab
54+
----------------------------
55+
56+
Google Colab is a commonly used runtime for pandas users. Below are concise, copy-pasteable ways to load data into pandas while working in Colab.
57+
58+
Uploading files from your local machine
59+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
61+
Use the Colab file upload widget and then read the uploaded file with :func:`pandas.read_csv`:
62+
63+
.. code-block:: python
64+
65+
from google.colab import files
66+
import pandas as pd
67+
68+
uploaded = files.upload() # pick file(s) from your machine
69+
df = pd.read_csv('your_file.csv')
70+
71+
72+
Mounting Google Drive
73+
^^^^^^^^^^^^^^^^^^^^^^
74+
75+
To access files you stored in Google Drive:
76+
77+
.. code-block:: python
78+
79+
from google.colab import drive
80+
import pandas as pd
81+
82+
drive.mount('/content/drive')
83+
df = pd.read_csv('/content/drive/MyDrive/path/to/your_file.csv')
84+
85+
86+
Downloading public files (gdown)
87+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88+
89+
For files shared via Google Drive links or hosted online, ``gdown`` is handy:
90+
91+
.. code-block:: python
92+
93+
!pip install gdown
94+
import gdown
95+
import pandas as pd
96+
97+
url = 'https://drive.google.com/uc?id=<FILE_ID>'
98+
output = 'file.csv'
99+
gdown.download(url, output, quiet=False)
100+
df = pd.read_csv(output)
101+
102+
103+
Reading Google Sheets
104+
^^^^^^^^^^^^^^^^^^^^^
105+
106+
If a Google Sheet is published to CSV you can read it directly:
107+
108+
.. code-block:: python
109+
110+
import pandas as pd
111+
112+
csv_url = 'https://docs.google.com/spreadsheets/d/<SHEET_ID>/export?format=csv&gid=<GID>'
113+
df = pd.read_csv(csv_url)
114+
115+
51116
The workhorse function for reading text files (a.k.a. flat files) is
52117
:func:`read_csv`. See the :ref:`cookbook<cookbook.csv>` for some advanced strategies.
53118

0 commit comments

Comments
 (0)