Open In Colab

Homework 3: Intermediate Pandas#

Practice advanced data manipulation with Pandas.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Problem 1: GroupBy Operations#

Analyze batch reactor data from multiple production runs.

# Load batch data from URL
url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw03_batch_data.csv"
batch_data = pd.read_csv(url)
batch_data.head()
batch_id reactor shift temperature pressure feed_rate conversion yield impurity
0 B0001 R3 night 436.6 1.89 145.0 0.775 0.650 0.089
1 B0002 R2 night 431.7 1.92 128.6 0.816 0.880 0.073
2 B0003 R2 night 375.8 3.69 58.9 0.846 0.793 0.087
3 B0004 R3 day 367.1 1.08 91.8 0.796 0.739 0.075
4 B0005 R3 night 416.9 1.42 137.9 0.603 0.562 0.150

1a. Calculate the mean yield and impurity for each reactor.

# Your code here

1b. Calculate the mean yield grouped by both reactor AND shift. Which combination performs best?

# Your code here

1c. Use .agg() to calculate multiple statistics (mean, std, min, max) for yield by reactor.

# Your code here

Problem 2: Merging DataFrames#

Combine data from different sources.

# Load reactor specifications from URL
url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw03_reactor_specs.csv"
reactor_specs = pd.read_csv(url)

# Quality control results (small dataset for merge demonstration)
qc_results = pd.DataFrame({
    'batch_id': ['B0001', 'B0002', 'B0003', 'B0005', 'B0010'],
    'passed_qc': [True, True, False, True, True],
    'qc_notes': ['OK', 'OK', 'Color off-spec', 'OK', 'OK']
})

print("Reactor specs:")
print(reactor_specs)
print("\nQC results:")
print(qc_results)
Reactor specs:
  reactor  volume_L  max_temp  age_years
0      R1      1000       500          8
1      R2      1500       480          7
2      R3       800       520          6

QC results:
  batch_id  passed_qc        qc_notes
0    B0001       True              OK
1    B0002       True              OK
2    B0003      False  Color off-spec
3    B0005       True              OK
4    B0010       True              OK

2a. Merge batch_data with reactor_specs to add reactor volume to each batch. How many batches don’t have matching reactor specs?

# Your code here

2b. Merge batch_data with qc_results using a left join. Fill missing QC values appropriately.

# Your code here

2c. Is there a relationship between reactor age (age_years) and yield? Calculate the correlation.

# Your code here

Problem 3: Pivot Tables and Reshaping#

3a. Create a pivot table showing mean yield for each reactor (rows) and shift (columns).

# Your code here

3b. Create temperature bins (340-345, 345-350, 350-355, 355-360) and analyze yield by temperature bin.

# Your code here

3c. Based on your analysis, write a brief recommendation for improving production yield.

Your answer here: