Nov 16, 2026 | 1058 words | 11 min read
11.1.2. PCA: Data Manipulation#
Required Files#
For this assignment:
Download the
templateand save as 11_1_2_username.py.Download the CSV file
metal_mass_volume.csvand save locally (same file name) for importing and analyzing the data.
Reminder
Review and reference the Python Style Guide to create a professionally formatted code file. For example, remember to provide detailed comments throughout your code so anyone can read them and understand what your program does!
Instructions#
Write a Python script that calculates density using the mass and volume data in
metal_mass_volume.csv. You will perform this calculation three ways: (0) a plain
Python loop, (1) numpy arrays, and (2) a pandas DataFrame. The goal isn’t just to
get the same answer three times; it’s to understand why numpy and pandas exist,
how pandas is actually built on top of numpy, and when each tool is the right
choice.
For this assignment, import the following:
import csv
import time
import numpy as np
import pandas as pd
The equation for density (\(\rho\)) is shown below:
where:
\(\rho\) is density
\(m\) is mass
\(V\) is volume
Part 0: Baseline – Plain Python Loop#
Load the data from
metal_mass_volume.csvinto a list of dictionaries (see provided code) – do not use pandas or NumPy here.
CSV_PATH = "metal_mass_volume.csv"
with open(CSV_PATH, newline="") as f:
reader = csv.DictReader(f)
rows = list(reader)
Print “Original Data - For Loop:” and then display the list
rowson a new line.Write a
forloop that calculates density for each row and stores the results in a list calleddensity_loop. Round the calculated density values to the one decimal place.Once the for loop finishes, print “Density (Loop):” and, on a new line, display
density_loop.Use
time.perf_counter()to measure how long this loop-based calculation takes. See the example code below to understand how to usetime.perf_counter()in your script. Save the result asloop_timeand print (6 decimal places).
start = time.perf_counter()
a = 3 + 5
b = 7 - 2
c = a * b
end = time.perf_counter()
execution_time = start - end
Execution time will be your performance and code-complexity baseline for comparison later.
Part 1: NumPy Arrays#
Load data from
metal_mass_volume.csvinto a pandas DataFrame callednumpy_df. (Note: we use pandas just to load the CSV conveniently; the calculation itself will use only NumPy.)Print “Original Data - NumPy:” and display the DataFrame.
Extract the columns
Mass_gandVolume_cm3fromnumpy_dfand convert them to NumPy arrays calledmass_npandvolume_np.Calculate density (
density_np) using vectorized NumPy division (round to one decimal place).Display “Density (numpy):” and then print
density_npon a new line.Time this calculation the same way you timed the for loop in Part 0. Save the result as
numpy_timeand print (6 decimal places).
Part 2: Pandas DataFrame#
Load the data from
metal_mass_volume.csvinto a DataFrame calledpandas_df.Print “Original Data - Pandas:” and display the DataFrame.
Calculate density directly using DataFrame column arithmetic. Set the column header for the density values to “Density_g/cm3.” *(Note: denisty values should be calculated and added as a new column to
pandas_dfin one line of code).Time this calculation using
time.perf_counter(). Save the result aspandas_timeand print (6 decimal places).Print “Density (Pandas):” and display
density_pdon a new line.
In Parts 3 and 4, answer the questions in your head or with a study partner to compare each calculation method (loop, NumPy, Pandas).
Part 3: Pandas Is Built on NumPy – Show Your Work#
Add code that demonstrates the relationship between the NumPy and Pandas libraries:
Print the results of
type(mass_np)andtype(pandas_df['Mass_g'].values)(see sample output below). What types are returned? How do they compare?Run
pandas_df['Mass_g'].to_numpy()and confirm it matches themass_nparray usingnp.array_equal().Compare
pandas_df['Mass_g'].dtypetomass_np.dtype. Are they the same?Use
np.allclose()to confirm the density values calculated in Part 1 (NumPy) and Part 2 (Pandas) are numerically identical.
Part 4: Where They Differ#
NumPy arrays and Pandas DataFrames aren’t just two syntaxes for the same thing; they behave differently in a few important ways. Explore this with code:
Indexing/labels: Sort
pandas_dfbyDensity_g/cm3using.sort_values(). Printpandas_dfand observe what happens to the row index. Then sort the NumPydensity_nparray usingnp.sort(). What information is lost when you sort a plain array that isn’t lost when you sort a DataFrame?Missing data: Manually set the first density value in
mass_npand the corresponding value inpandas_df['Mass_g']tonp.nan. Recalculate density both ways. What happens to the result in each case? Then usepandas_df['Density_g/cm3'].isna()to detect the missing value in pandas – is there an equally simple way to do this with a raw NumPy array?Run Time: Compare the run times for each method (
loop_time,numpy_time,pandas_time). Which method would you use to makes calculations and why?
Note
Looking at the run times, the loop and NumPy were both fast, with NumPy edging out the loop thanks to vectorized, pre-compiled operations. Pandas was slightly slower since it adds bookkeeping (index, labels) on top of the same NumPy math underneath – but that gap is a fraction of a millisecond and won’t matter until you’re working with huge datasets.
Pandas is still the right tool to use going forward. It’s built on NumPy, so you
keep NumPy’s speed, but you also get filtering (df[df['Density_g/cm3'] > 8]), cleaning
(.dropna(), .fillna()), labeled sorting, and plotting (df.plot()) – none of which
raw NumPy arrays support. You’re trading a negligible amount of speed for a much more
capable toolset.
Rule of thumb: use Pandas by default for calculations, filtering, cleaning, sorting, and plotting. Reach for plain NumPy only when you have a massive dataset, don’t need labels, and raw speed is the only priority.
Use the sample inputs and outputs below to test your program.
Sample Output#
Sample Output
Gradescope Submission#
Save Deliverables: Ensure all required assignment files are saved and named exactly as specified in the assignment instructions. If a template file name contains the word
template, replace only that word with your Purdue username. When a task lists more than one deliverable, such as a workbook and a report, submit each deliverable to its own matching Gradescope assignment instead of combining them into one upload.Navigate to Gradescope: Log in to Gradescope. From your Dashboard, open the assignment.
Upload your work: Select the Upload submission method. Drag and drop all required files (or a compressed
.zipfolder) into Gradescope, and select Upload.Confirm Submission: When your upload is successful, you will see a confirmation message on your screen and receive an automated email receipt.
Add Team Members (if applicable): If this is a team assignment, add all team members to the submission. Each teammate should confirm they can see the submission in their own Gradescope account. For help, visit Gradescope’s documentation on adding team members.
Review Autograder Results: Wait a few minutes for the autograder to evaluate the relevant files. Scroll down to view any failed test cases. If you need assistance interpreting the output or errors, ask your teaching team.
Resubmit if Necessary: You can edit your files and select Resubmit as many times as you need before the assignment deadline. Only your final, active submission is graded. (Note: If you need to resubmit, you must upload all required files for the assignment again).
For a detailed walkthrough, review the official Gradescope Submission Guide.