Skip to content

Commit d0d9b32

Browse files
Explanation on loading files to Google Colab added
1 parent 606a61b commit d0d9b32

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

doc/source/user_guide/io.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6521,3 +6521,86 @@ The files ``test.pkl.compress``, ``test.parquet`` and ``test.feather`` took the
65216521
24458940 Oct 10 06:44 test_table.hdf
65226522
24458940 Oct 10 06:44 test_table_compress.hdf
65236523
6524+
=====================================================================================================================
6525+
LOADING PANDA IN GOOGLE COLAB
6526+
=====================================================================================================================
6527+
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
6580+
Python data structures within the notebook.
6581+
6582+
From a dictionary
6583+
data = {
6584+
'Name': ['Alice', 'Bob', 'Charlie'],
6585+
'Age': [25, 30, 35],
6586+
'City': ['New York', 'Los Angeles', 'Chicago']
6587+
}
6588+
6589+
df = pd.DataFrame(data)
6590+
print(df)
6591+
6592+
From a NumPy array
6593+
import numpy as np
6594+
6595+
my_data = np.array([
6596+
[0, 3],
6597+
[10, 7],
6598+
[20, 9],
6599+
[30, 14],
6600+
[40, 15]
6601+
])
6602+
6603+
column_names = ['temperature', 'activity']
6604+
6605+
df = pd.DataFrame(data=my_data, columns=column_names)
6606+
print(df)

0 commit comments

Comments
 (0)