Lesson 4 · Mastering Pandas
Mastering DataFrames in Python Pandas: Creating & Inspecting for Data Science
Welcome to this hands-on session! We will start by introducing DataFrames, the heart of pandas. You will learn to create, explore, and understand tabular…
- CourseMastering Pandas
- Lesson4 of 44
- Video13 min
- FormatJupyter notebook · 13 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbCreating and Inspecting DataFrames in Pandas#
Welcome to this hands-on session! We will start by introducing DataFrames, the heart of pandas. You will learn to create, explore, and understand tabular data using simple, real-world examples. Let us dive in!
import warnings
warnings.filterwarnings('ignore')
# Data setup (Iris Dataset)
import pandas as pd
import numpy as np
np.random.seed(42)
url = 'https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv'
df = pd.read_csv(url)
print(df.shape)
print(df.head(3))
What is a DataFrame?#
A DataFrame is like a table or spreadsheet in your notebook. Each column has a name and can hold different types of data. You can analyze, filter, sort, and visualize data easily with DataFrames.
# Quick tour of DataFrame info
df.info()
# Examining column names and data types
print('Column names:', list(df.columns))
print('Data types:')
print(df.dtypes)
# Creating a DataFrame from scratch
data = {
'name': ['Elena', 'Ravi', 'Lee'],
'score': [90, 88, 70],
'passed': [True, True, False]
}
df_simple = pd.DataFrame(data)
print(df_simple)
Summarizing DataFrames#
Pandas can help summarize columns using built-in functions. This helps you quickly see the range of the numeric columns or counts of unique values for text.
# Get summary statistics for numeric columns
print(df.describe())
# Value counts for a text column
print(df['species'].value_counts())
Indexing and Selecting Data#
Pandas lets you grab specific rows or columns using labels or numbers. This way, you can focus on just the part of the data you need.
# Select a single column (becomes a Series)
sepal_lengths = df['sepal_length']
print(sepal_lengths.head())
# Select multiple columns by name
subset = df[['sepal_length', 'species']]
print(subset.head())
# Selecting rows by position using iloc
row5 = df.iloc[4]
print(row5)
# Filtering rows with a condition
long_flowers = df[df['sepal_length'] > 7.0]
print(long_flowers.head())
Modifying DataFrames#
You can add new columns, change values, or drop data as you clean and transform your data.
# Add a new column based on a calculation
df['sepal_ratio'] = df['sepal_length'] / df['sepal_width']
print(df[['sepal_length', 'sepal_width', 'sepal_ratio']].head())
# Deleting a column
df = df.drop('sepal_ratio', axis=1)
print(df.head(2))
Quick Practice: Exploring DataFrames#
Try using shape, head, and info on the df_simple DataFrame you made earlier. What do you notice compared to the main iris DataFrame?
# Input practice: Name a column you see in df
column_guess = input('Type any column name from the main iris DataFrame: ')
if column_guess in df.columns:
print('Nice! That column exists.')
else:
print('Check spelling and try again.')
Recap: What We Covered#
- Creating DataFrames from real files and from scratch
- Inspecting and summarizing columns
- Selecting and filtering with labels and numbers
- Adding and deleting columns to transform your data
This is the foundation for all pandas analysis!
What next? Try these:#
- Experiment with loading different datasets.
- Practice selecting and filtering columns and rows.
- Visualize your DataFrame using .plot().
If you enjoyed this lesson, subscribe for more pandas tips and tutorials!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



