Skip to content

Commit 8e13d0c

Browse files
committed
DOC: Add section on loading Data to Pandas in Google Colab
1 parent e95948f commit 8e13d0c

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
@@ -51,6 +51,91 @@ CSV & text files
5151
The workhorse function for reading text files (a.k.a. flat files) is
5252
:func:`read_csv`. See the :ref:`cookbook<cookbook.csv>` for some advanced strategies.
5353

54+
55+
56+
How to Load Data to Pandas in Google Colab
57+
------------------------------------------
58+
59+
Google Colab is a cloud based platform which allows users to write and execute Python code
60+
because Colab runs on remote servers, local files on your computer are not directly accessible
61+
Pandas users often need to take some extra steps to read data.
62+
63+
64+
Common Data Sources in Colab
65+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66+
67+
+----------------------+--------------------------------------+
68+
| Source | Recommended Method |
69+
+======================+======================================+
70+
| Local file upload | ``files.upload()`` |
71+
+----------------------+--------------------------------------+
72+
| Google Drive | ``drive.mount('/content/drive')`` |
73+
+----------------------+--------------------------------------+
74+
| Remote dataset (URL) | ``pd.read_csv(url)`` |
75+
+----------------------+--------------------------------------+
76+
77+
**1. Upload local files manually**
78+
79+
For small files or one-time uploads, you can upload directly from your
80+
computer using Colab’s file dialog.
81+
82+
.. code-block:: python
83+
84+
from google.colab import files
85+
import pandas as pd
86+
87+
uploaded = files.upload() # Choose a file from your computer
88+
df = pd.read_csv("your_file.csv")
89+
df.head()
90+
91+
**2. Mount Google Drive**
92+
93+
For larger or persistent datasets, mounting Google Drive provides access to
94+
files that stay available between Colab sessions.
95+
96+
.. code-block:: python
97+
98+
from google.colab import drive
99+
drive.mount("/content/drive")
100+
101+
df = pd.read_csv("/content/drive/MyDrive/data/your_file.csv")
102+
df.head()
103+
104+
**3. Read from a URL**
105+
106+
You can also read data directly from public GitHub repositories, Google Sheets, Kaggle datasets, or cloud storage services.
107+
All of these ultimately provide a URL or accessible path to ``pd.read_csv()``.
108+
109+
.. code-block:: python
110+
111+
import pandas as pd
112+
url = "https://example.com/data.csv"
113+
df = pd.read_csv(url)
114+
df.head()
115+
116+
Example using a public dataset:
117+
118+
.. code-block:: python
119+
120+
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv"
121+
df = pd.read_csv(url)
122+
df.head()
123+
124+
.. tip::
125+
126+
If you receive a ``FileNotFoundError`` after uploading, verify that the
127+
filename matches exactly (case-sensitive) and that the file was uploaded
128+
to the current Colab session.
129+
130+
.. note::
131+
132+
Files uploaded manually exist only for the duration of the Colab session.
133+
Mount Google Drive to keep data available between sessions.
134+
135+
For more details, see the official
136+
`Google Colab guide on file access <https://colab.research.google.com/notebooks/io.ipynb>`_.
137+
138+
54139
Parsing options
55140
'''''''''''''''
56141

0 commit comments

Comments
 (0)