Lesson 7 · Data analytics zero to hero
Pandas Series & DataFrames from Scratch | Data Analytics #7
And the start of a multi-part pandas block: Series, DataFrames, and reading a real CSV file. We'll load the real mtcars dataset, 1974 Motor Trend car road…
- CourseData analytics zero to hero
- Lesson7 of 30
- Video15 min
- FormatJupyter notebook · 15 code cells
- Data1 dataset
What you'll learn
Datasets used in this lesson
Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.
- mtcars.csv preview1.7 KB
📓 Full notebook
Download .ipynbData Analytics Zero to Hero, Video 7: Pandas Series and DataFrames#
- Video seven of the 30-part series, and the start of a multi-part pandas block: Series, DataFrames, and reading a real CSV file.
- We'll load the real mtcars dataset, 1974 Motor Trend car road tests, straight into pandas.
- Let's jump straight in.
Before You Start#
- Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
- Install pandas if you haven't already:
pip install pandas. - Place mtcars.csv in the same folder as this notebook.
Part 1: The Series#
import pandas as pd
mpg = pd.Series([21.0, 22.8, 21.4, 18.7, 18.1])
print(mpg)
print(mpg.values)
print(mpg.index)
mpg_named = pd.Series([21.0, 22.8, 21.4], index=['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive'])
print(mpg_named)
print(mpg_named['Datsun 710'])
Part 2: Building a DataFrame#
data = {
'model': ['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive'],
'mpg': [21.0, 22.8, 21.4],
'cyl': [6, 4, 6]
}
df = pd.DataFrame(data)
print(df)
Part 3: Reading a Real CSV File#
df = pd.read_csv('mtcars.csv')
print(df.head())
print(df.tail(3))
print(df.shape)
print(df.columns)
print(df.dtypes)
df.info()
print(df.describe())
Part 4: Selecting Columns#
mpg_col = df['mpg']
print(type(mpg_col))
print(mpg_col.head())
subset = df[['model', 'mpg', 'hp']]
print(type(subset))
print(subset.head())
Part 5: Selecting Rows with loc and iloc#
print(df.iloc[0])
print(df.iloc[0:3])
df_named = df.set_index('model')
print(df_named.loc['Fiat 128'])
print(df_named.loc['Fiat 128', 'mpg'])
Part 6: Boolean Filtering#
efficient = df[df['mpg'] > 25]
print(efficient[['model', 'mpg']])
v8_efficient = df[(df['cyl'] == 8) & (df['mpg'] > 15)]
print(v8_efficient[['model', 'cyl', 'mpg']])
Part 7: Adding Columns and Sorting#
df['kml'] = df['mpg'] * 0.425
print(df[['model', 'mpg', 'kml']].head())
top_mpg = df.sort_values('mpg', ascending=False)
print(top_mpg[['model', 'mpg']].head())
Wrap-Up: What You Learned#
- The Series: a single labeled column, and the building block of every DataFrame.
- Building a DataFrame from a dictionary, and reading one directly from a real CSV file with read_csv.
- Exploring a new DataFrame: head, tail, shape, columns, dtypes, info, and describe.
- Selecting columns with square brackets, and rows with loc and iloc.
- Boolean filtering, including combining multiple conditions.
- Adding a new computed column, and sorting with sort_values.
- This is the pandas foundation everything else in this series builds on. Video eight moves into real-world data cleaning: missing values, duplicates, and data types. Subscribe so it lands automatically see you there.
🧠 Test yourself
Pick an answer to check it. Your best score is saved in this browser.
What does
df[['model', 'mpg']](double brackets) return?A list of column names inside the brackets selects several columns and returns a DataFrame. Single brackets with one name return a Series.
What is the difference between
ilocandloc?iloc[0]means the first row by position, whileloc['Fiat 128']looks up a row by its index label.Which line correctly selects cars with 8 cylinders AND mpg above 15?
Pandas combines conditions with & (and) or | (or), and each condition needs its own parentheses.
What does
df.shapereturn for a DataFrame with 32 rows and 12 columns?shape is a tuple of (rows, columns).
What does
df.sort_values('mpg', ascending=False)do?It returns a new sorted DataFrame with the largest mpg values first.
💻 Practice exercises
Try each one in the notebook or in your own editor before opening the solution.
Build a DataFrame from a dict with columns model, mpg, cyl for 3 cars. Print it, its shape, and the dtypes.
💡 Hint
Use pd.DataFrame(data).
✅ Show solution
import pandas as pd data = { 'model': ['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive'], 'mpg': [21.0, 22.8, 21.4], 'cyl': [6, 4, 6] } df = pd.DataFrame(data) print(df) print(df.shape) print(df.dtypes)
Using a small DataFrame of cars, select only cars with mpg above 21 and show just the model and mpg columns, sorted from highest mpg.
💡 Hint
Filter with a boolean condition, then sort_values.
✅ Show solution
import pandas as pd df = pd.DataFrame({ 'model': ['Mazda RX4', 'Datsun 710', 'Hornet 4 Drive', 'Fiat 128', 'Valiant'], 'mpg': [21.0, 22.8, 21.4, 32.4, 18.1], 'cyl': [6, 4, 6, 4, 6] }) efficient = df[df['mpg'] > 21] print(efficient[['model', 'mpg']].sort_values('mpg', ascending=False))
With the same kind of small cars DataFrame, add a column
kml = mpg * 0.425, setmodelas the index, and uselocto print the kml of 'Fiat 128'. Also print the first row with iloc.💡 Hint
Use set_index('model') then .loc['Fiat 128', 'kml'].
✅ Show solution
import pandas as pd df = pd.DataFrame({ 'model': ['Mazda RX4', 'Datsun 710', 'Fiat 128'], 'mpg': [21.0, 22.8, 32.4], 'cyl': [6, 4, 4] }) df['kml'] = df['mpg'] * 0.425 named = df.set_index('model') print(named.loc['Fiat 128', 'kml']) print(df.iloc[0])
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



