Why Loading Data with Pandas is Essential
Data is rarely created directly inside your code. In data science, you load data from external sources—like files on disk, databases, or API streams. Pandas is the industry-standard library that transforms flat files (CSV, Excel) into highly functional, tabular structures called DataFrames, enabling you to inspect, filter, clean, and analyze datasets with ease.
14.1 Importing Libraries & Alias Shortening
Before using any library, you must import it into your runtime namespace. The standard import command is simply import pandas. However, typing pandas before every single function call is tedious. We use the as keyword to assign a shorter, cleaner alias.
# The industry standard alias for pandas
import pandas as pd
# Load files using the alias 'pd'
df = pd.read_csv("dataset.csv")
Note on Alias Conventions: While you can legally use any alias (e.g. import pandas as banana), the data science community universally uses pd. Sticking to standard conventions makes your code readable and collaborative.
Type an alias to import the library under your custom keyword, then run the test statement to see if Python recognizes your namespace variable.
pd, pandas, or banana14.2 Loading Datasets: read_csv() vs read_excel()
Pandas provides optimized functions to load different file types directly into DataFrames. The two most common are:
pd.read_csv("path/file.csv"): Loads comma-separated value files (flat text tables).pd.read_excel("path/file.xlsx"): Loads binary Microsoft Excel spreadsheets.
Once loaded, you can call the .head() method to inspect the first 5 rows and verify the formatting is correct.
import pandas as pd
# Load music albums dataset from CSV
df = pd.read_csv("music_albums.csv")
# Print first 2 rows of the DataFrame
print(df.head(2))
Try Loading Data: Click "Try It Yourself" to run Pandas data loading. Pyodide parses the CSV file dynamically and loads it into a standard 2D DataFrame table.
Choose a dataset file type from the dropdown, execute the load command, and run df.head() to render the preview table.
14.3 DataFrame Structure, Slicing & Element Access
A DataFrame is a 2D tabular structure containing rows and labeled columns. You can access its data using brackets or index-based methods.
import pandas as pd
# Initialize data and DataFrame
df = pd.read_csv("music_albums.csv")
# 1. Extract multiple columns using double brackets
print("--- Extracted Columns ---")
print(df[['Artist', 'Released']])
# 2. Access elements using integer offset (.iloc)
print("\nFirst row, third column (.iloc[0, 2]):")
print(df.iloc[0, 2])
# 3. Access elements using labels (.loc)
print("\nFirst row, column 'Artist' (.loc[0, 'Artist']):")
print(df.loc[0, 'Artist'])
Try Slicing DataFrames: Click "Try It Yourself" to run column extraction and row-column cell access via iloc and loc offsets.
Brackets Selection (Columns)
To extract specific columns, pass the column label or list of labels in brackets:
df[['Artist']]: Extracts one column as a new DataFrame (double brackets).df[['Artist', 'Released']]: Extracts multiple columns as a new DataFrame.
df.ix[] for cell selection. .ix is deprecated and removed in modern Pandas. Always use .loc (label-based) or .iloc` (integer index-based) instead.
Elements Access: .loc vs .iloc
.iloc[row_idx, col_idx]: Accesses cells using standard integer offsets, e.g.,df.iloc[0, 2](first row, third column)..loc[row_label, col_label]: Accesses cells using labels/names, e.g.,df.loc[0, 'Artist'](first row of column named 'Artist').
Click individual cells, row headers, or column headers on the table. The generator will construct the exact Pandas selection/slicing statement and show the output slice!
| Artist | Album | Released | Genre | |
|---|---|---|---|---|
| 0 | Michael Jackson | Thriller | 1982 | Pop/Rock |
| 1 | AC/DC | Back in Black | 1980 | Hard Rock |
| 2 | Pink Floyd | Dark Side of the Moon | 1973 | Prog Rock |
| 3 | Whitney Houston | The Bodyguard | 1992 | R&B/Pop |
Practice Quiz
Validate your understanding of importing libraries, loading file structures, and selecting slices in Pandas.