\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch % NOTE: do NOT define \bar -- it is the built-in LaTeX accent used by \bar{x}, % \bar{y} (sample means). Redefining it made those render as the word "bar". \newcommand\barunit{\unit{bar}} \end{aligned}\end{align} \]

Nov 16, 2026 | 1058 words | 11 min read

11.1.2. PCA: Data Manipulation#

Required Files#

For this assignment:

  1. Download the template and save as 11_1_2_username.py.

  2. Download the CSV file metal_mass_volume.csv and 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:

(11.1)#\[\rho = \frac{m}{V}\]

where:

  • \(\rho\) is density

  • \(m\) is mass

  • \(V\) is volume


Part 0: Baseline – Plain Python Loop#

  1. Load the data from metal_mass_volume.csv into 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)
  1. Print “Original Data - For Loop:” and then display the list rows on a new line.

  2. Write a for loop that calculates density for each row and stores the results in a list called density_loop. Round the calculated density values to the one decimal place.

  3. Once the for loop finishes, print “Density (Loop):” and, on a new line, display density_loop.

  4. Use time.perf_counter() to measure how long this loop-based calculation takes. See the example code below to understand how to use time.perf_counter() in your script. Save the result as loop_time and 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#

  1. Load data from metal_mass_volume.csv into a pandas DataFrame called numpy_df. (Note: we use pandas just to load the CSV conveniently; the calculation itself will use only NumPy.)

  2. Print “Original Data - NumPy:” and display the DataFrame.

  3. Extract the columns Mass_g and Volume_cm3 from numpy_df and convert them to NumPy arrays called mass_np and volume_np.

  4. Calculate density (density_np) using vectorized NumPy division (round to one decimal place).

  5. Display “Density (numpy):” and then print density_np on a new line.

  6. Time this calculation the same way you timed the for loop in Part 0. Save the result as numpy_time and print (6 decimal places).


Part 2: Pandas DataFrame#

  1. Load the data from metal_mass_volume.csv into a DataFrame called pandas_df.

  2. Print “Original Data - Pandas:” and display the DataFrame.

  3. 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_df in one line of code).

  4. Time this calculation using time.perf_counter(). Save the result as pandas_time and print (6 decimal places).

  5. Print “Density (Pandas):” and display density_pd on 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:

  1. Print the results of type(mass_np) and type(pandas_df['Mass_g'].values) (see sample output below). What types are returned? How do they compare?

  2. Run pandas_df['Mass_g'].to_numpy() and confirm it matches the mass_np array using np.array_equal().

  3. Compare pandas_df['Mass_g'].dtype to mass_np.dtype. Are they the same?

  4. 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:

  1. Indexing/labels: Sort pandas_df by Density_g/cm3 using .sort_values(). Print pandas_df and observe what happens to the row index. Then sort the NumPy density_np array using np.sort(). What information is lost when you sort a plain array that isn’t lost when you sort a DataFrame?

  2. Missing data: Manually set the first density value in mass_np and the corresponding value in pandas_df['Mass_g'] to np.nan. Recalculate density both ways. What happens to the result in each case? Then use pandas_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?

  3. 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

$ python3 11_1_2_username.pyOriginal Data - For Loop:[{'Sample': 'A', 'Mass_g': '5.4', 'Volume_cm3': '2'}, {'Sample': 'B', 'Mass_g': '9', 'Volume_cm3': '2'}, {'Sample': 'C', 'Mass_g': '23.5', 'Volume_cm3': '3'}, {'Sample': 'D', 'Mass_g': '25.8', 'Volume_cm3': '3'}, {'Sample': 'E', 'Mass_g': '44.8', 'Volume_cm3': '5'}, {'Sample': 'F', 'Mass_g': '62.4', 'Volume_cm3': '8'}]Density (loop):[2.7, 4.5, 7.8, 8.6, 9.0, 7.8]Loop execution time (s): 0.000010Original Data - NumPy: Sample Mass_g Volume_cm30 A 5.4 21 B 9.0 22 C 23.5 33 D 25.8 34 E 44.8 55 F 62.4 8Density (numpy):[2.7 4.5 7.8 8.6 9. 7.8]NumPy execution time (s): 0.000016Original Data - Pandas: Sample Mass_g Volume_cm30 A 5.4 21 B 9.0 22 C 23.5 33 D 25.8 34 E 44.8 55 F 62.4 8Density (Pandas): Sample Mass_g Volume_cm3 Density_g/cm30 A 5.4 2 2.71 B 9.0 2 4.52 C 23.5 3 7.83 D 25.8 3 8.64 E 44.8 5 9.05 F 62.4 8 7.8pandas execution time (s): 0.000211Equivalence Checks:type(mass_np): <class 'numpy.ndarray'>mass.dtype(): float64type(pandas_df['Mass_g'].values): <class 'numpy.ndarray'>mass arrays equal (NumPy vs pandas): Truedtype match (pandas vs NumPy): Truedensity values allclose (NumPy vs pandas): TrueSorted by Density (pandas): Sample Mass_g Volume_cm3 Density_g/cm30 A 5.4 2 2.71 B 9.0 2 4.52 C 23.5 3 7.85 F 62.4 8 7.83 D 25.8 3 8.64 E 44.8 5 9.0Sorted Density (NumPy array):[2.7 4.5 7.8 7.8 8.6 9. ]Density with Missing Mass (NumPy):[ nan 4.5 7.83333333 8.6 8.96 7.8 ]Density with Missing Mass (pandas): Sample Mass_g Volume_cm3 Density_g/cm30 A NaN 2 NaN1 B 9.0 2 4.52 C 23.5 3 7.83 D 25.8 3 8.64 E 44.8 5 9.05 F 62.4 8 7.8NaN detected via pandas .isna(): True

Gradescope Submission#

  1. 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.

  2. Navigate to Gradescope: Log in to Gradescope. From your Dashboard, open the assignment.

  3. Upload your work: Select the Upload submission method. Drag and drop all required files (or a compressed .zip folder) into Gradescope, and select Upload.

  4. Confirm Submission: When your upload is successful, you will see a confirmation message on your screen and receive an automated email receipt.

  5. 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.

  6. 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.

  7. 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.