A Programming Assignment of Shinade T. Cancino from 2ECEC for her class, Advanced Computer Programming.
This repository contains Python programs using Pandas to practice loading datasets, displaying data, subsetting, slicing, and indexing. The problems are:
1. Loading and Displaying the Dataset — loads cars.csv
into a DataFrame and displays the first and last five rows.
2. Subsetting, Slicing, and Indexing — extracts specific information from the DataFrame using Pandas methods.
This README provides step-by-step explanations for every line of code, making the logic and flow clear.
- Open Jupyter Notebook.
- Ensure
cars.csv
is in the same directory as your notebook. - Run the cells from top to bottom:
- Press Shift + Enter on each cell, or
- Click the play icon.
Tip
Make sure your Python kernel is active.
Outputs will appear inline after each cell.
Load the dataset
cars.csv
into a Pandas DataFrame namedcars
and display its first and last five rows.
Full Code:
import pandas as pd #Import the pandas library
cars = pd.read_csv("cars.csv") #Load the dataset into a DataFrame called cars
cars #Display the entire DataFrame
cars.head() #Display the first five rows of the DataFrame
cars.tail() #Display the last five rows of the DataFrame
Cell 1: Import Pandas and Load Dataset
import pandas as pd
cars = pd.read_csv("cars.csv")
cars
Explanation:
import pandas as pd
→ Imports the Pandas library to work with tabular data.pd.read_csv("cars.csv")
→ Reads the CSV file into a Pandas DataFrame.cars = ...
→ Stores the dataset in the variable cars.cars
→ Displays the full DataFrame in the notebook.
Cell 2: Display First 5 Rows
cars.head()
Explanation:
cars.head()
→ Shows the first five rows of the DataFrame.- Useful to quickly preview the top of the dataset
Cell 3: Display Last 5 Rows
cars.tail()
Explanation:
cars.tail()
→ Shows the last five rows of the DataFrame.- Useful to inspect the bottom of the dataset.
Flow: Dataset loaded → first 5 rows displayed → last 5 rows displayed.
Example Outputs:
- First five rows

- Last five rows

Extract specific information from the
cars
DataFrame using slicing, indexing, and conditional selection.
Full Code:
import pandas as pd
cars = pd.read_csv("cars.csv") #Load the dataset into a DataFrame called cars
cars #Display the entire DataFrame
cars.iloc[0:5, ::2] # a. Display the first five rows with odd-numbered columns
# ::2 means select every other column starting from index 0
(cars[cars['Model']=='Mazda RX4']) # b. Display the row that contains the 'Model' of 'Mazda RX4'
cylinders = cars.loc[cars['Model'] == 'Camaro Z28', 'cyl'].values[0] # c. Display how many cylinders does Camaro Z28 have
print("Camaro Z28 has", cylinders, "cylinders.")
(cars.loc[cars['Model'].isin(['Mazda RX4 Wag', 'Ford Pantera L', 'Honda Civic']), ['Model', 'cyl', 'gear']]) # d. Display the cylinders and gear type for Mazda RX4 Wag, Ford Pantera L, and Honda Civic
Cell 1: Import pandas
import pandas as pd
Explanation:
import pandas as pd
imports the pandas library, a powerful tool for working with tabular data.pd
is the alias commonly used to call pandas functions.
Cell 2: Load dataset into a DataFrame
cars = pd.read_csv("cars.csv")
cars
Explanation:
pd.read_csv("cars.csv")
reads the CSV file into a DataFrame, which is like a table in Python.cars = ...
stores the dataset in the variablecars
.cars
on the last line of the cell displays the full DataFrame in Jupyter Notebook.
Cell 3a: Display the first five rows with odd-numbered columns
cars.iloc[0:5, ::2]
Explanation:
cars.iloc[...]
allows index-based selection of rows and columns.0:5
selects the first five rows (row indices 0 to 4).::2
selects every other column starting from index 0, i.e., odd-numbered columns.
The result shows a subset of the DataFrame with only the specified rows and columns.
Cell 3b: Display the row for Mazda RX4
(cars[cars['Model']=='Mazda RX4'])
Explanation:
cars['Model']
== 'Mazda RX4' → Creates a boolean mask where the model is "Mazda RX4".cars[...]
→ Filters the DataFrame to only that row.
The result is a DataFrame containing all columns for the
'Mazda RX4'
model.
Cell 3c: Find Cylinders for "Camaro Z28"
cylinders = cars.loc[cars['Model'] == 'Camaro Z28', 'cyl'].values[0]
print("Camaro Z28 has", cylinders, "cylinders.")
Explanation:
cars.loc[...]
allows label-based selection of rows and columns.cars['Model'] == 'Camaro Z28'
selects the row where Model is'Camaro Z28'
.'cyl'
selects the cylinders column..values[0]
→ Extracts the integer value of cylinders.print(...)
→ Displays the number of cylinders.
Cell 3d: Find Cylinders and Gear Type for Selected Models
(cars.loc[cars['Model'].isin(['Mazda RX4 Wag', 'Ford Pantera L', 'Honda Civic']), ['Model', 'cyl', 'gear']])
Explanation:
cars['Model'].isin([...])
creates a boolean mask for rows where the Model is one of the specified models.cars.loc[mask, ['Model', 'cyl', 'gear']]
selects the rows that match the mask and only the specified columns:'Model'
,'cyl'
, and'gear'
.- The resulting DataFrame shows the cylinders and gear type for
'Mazda RX4 Wag'
,'Ford Pantera L'
, and'Honda Civic'
.
Example Outputs:
-
First five rows with odd-numbered columns
-
Cylinders of Camaro Z28
-
Cylinders and gear type for Mazda RX4 Wag, Ford Pantera L, and Honda Civic
Version 2: The README has been completely revised to provide a cell-by-cell explanation of each code segment, making it easier for viewers to understand the flow and logic of the notebook.