You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Google Colab, pandas data can be loaded in multiple ways.
6528
-
You can either create a DataFrame directly from Python objects
6529
-
or import data from external sources such as local files,
6530
-
Google Drive, or public URLs.
6531
-
6532
-
Start by importing pandas:
6533
-
import pandas as pd
6534
-
6535
-
1. Loading data from external sources
6536
-
The most common way to load data into pandas is by reading external files such as CSV or Excel files. In Google Colab, this is typically done through file uploads, Google Drive integration, or public URLs.
6537
-
6538
-
A. Uploading files from your local computer (session-only)
6539
-
This method is suitable for small to medium-sized files. Note that uploaded files are temporary and will be lost when the Colab runtime disconnects.
6540
-
6541
-
from google.colab import files
6542
-
import io
6543
-
6544
-
uploaded = files.upload() # Opens a file picker dialog
6545
-
6546
-
for filename in uploaded.keys():
6547
-
df = pd.read_csv(io.BytesIO(uploaded[filename]))
6548
-
print(f'Uploaded file "{filename}" with {len(df)} rows.')
6549
-
6550
-
B. Loading files from Google Drive (persistent storage)
6551
-
Google Drive can be mounted into the Colab environment,
6552
-
allowing persistent access to files across sessions.
6553
-
6554
-
First, mount Google Drive:
6555
-
6556
-
from google.colab import drive
6557
-
drive.mount('/content/drive')
6558
-
6559
-
Follow the prompt to authenticate your Google account.
6560
-
Once mounted, your Drive will appear under /content/drive.
6561
-
6562
-
Next, read the file using its path:
6563
-
6564
-
# Example path: '/content/drive/My Drive/data_folder/my_data.csv'
6565
-
file_path = '/content/drive/My Drive/my_data.csv'
6566
-
df = pd.read_csv(file_path)
6567
-
print(df.head())
6568
-
6569
-
C. Loading data from a public URL
6570
-
If your dataset is hosted online (for example, on GitHub
6571
-
or a public data repository), pandas can read it directly
6572
-
from the URL:
6573
-
6574
-
url = 'https://raw.githubusercontent.com/...'
6575
-
df = pd.read_csv(url)
6576
-
print(df.head())
6577
-
6578
-
2. Creating a DataFrame from Python objects
6579
-
A DataFrame can also be created directly from in-memory
0 commit comments