Appendix C — Assignment C: Pandas
Instructions
The assignment is worth 100 points. See Canvas for the due date. There is an optional bonus question worth 10 points, so you can score a maximum of 110 (out of 100) points.
You may discuss approaches with classmates, but write your own code and explanations. Put Python computations in Code cells and write the final answer to each question in a Markdown cell.
Download the Assignment C project kit. Extract it and move the extracted
assignment-cfolder intostat303-1/assignments/, next toassignment-aandassignment-b. Check that you did not create a nestedassignment-c/assignment-c/folder. Openstat303-1in VS Code, openassignments/assignment-c/assignment_c.ipynb, and select the course.venvas its kernel. Pandas is already installed there from Assignment B.This page contains the complete assignment instructions. The starter notebook contains numbered answer spaces and supplied code. Keep those sections in order, answer every numbered part below, and add your name to the
authorline at the top of the notebook.The data files are in the kit’s
data/folder. Read them with relative path strings such as"data/social_indicator.txt"; the notebook runs from theassignment-cfolder, so these paths work without changes.You are not allowed to use a
forloop or any other kind of loop in this assignment.Submit one file,
assignment_c.html, to the Assignment C submission in Canvas. Prepare and Submit the HTML at the end of this page explains how to render it.Three of the 100 points are for properly formatting the assignment. The breakdown is as follows:
- Must be an HTML file rendered using Quarto (1 pt).
- There aren’t excessively long outputs of extraneous information (e.g. no printouts of entire data frames without good reason, there aren’t long sections of commented-out code, etc.) (1 pt).
- Final answers of each question are written in Markdown cells, and there is no unnecessary / redundant code or text (1 pt).
Generative AI. You may use generative AI tools (ChatGPT, Claude, GitHub Copilot, and similar) to understand concepts, interpret error messages, and debug code you have written. You may not use them to produce the code or written answers you submit. The test: you should be able to explain every line you submit and reproduce it without assistance. Disclose any use in the AI Use Disclosure section (Question 4). Honest disclosure earns points and carries no penalty on its own; undisclosed use is an academic integrity violation under the course syllabus.
When you place the kit, your assignments folder will look like this:
stat303-1/
├── .venv/
└── assignments/
├── assignment-a/
├── assignment-b/
└── assignment-c/ ← the extracted kit
├── assignment_c.ipynb
├── README.md
└── data/
├── conditions.csv
├── patients.csv
├── social_indicator.txt
└── stat303_survey.csv
Points Summary
| Section | Points |
|---|---|
| 1. GDP per capita and social indicators | 34 |
| 2. GDP per capita vs social indicators | 49 |
| 3. Medical data | 12 |
| 4. AI Use Disclosure | 2 |
| 5. Formatting (instruction 8 above) | 3 |
| Total | 100 |
| Bonus: survey data cleaning (optional) | 10 |
3. Medical Data (12 points)
Read the data sets data/conditions.csv and data/patients.csv. Suppose we are interested in studying patients with prediabetes condition. Do not drop or compute any missing values. In conditions.csv, the patient IDs are stored in column PATIENT, and the medical conditions are stored in column DESCRIPTION. In patients.csv, the patient IDs are stored in column Id.
3.1 Prediabetes patient IDs (4 points)
Print the patient IDs of all the patients with prediabetes condition.
3.2 Prediabetes patients (4 points)
Make a subset of the data with only prediabetes patients. How many prediabetes patients are there?
Hint: .isin()
4. AI Use Disclosure (2 points)
In the AI Use Disclosure section of your notebook, state whether generative AI tools were used to complete any part of this assignment.
If no AI was used, write: “I did not use generative AI tools on this assignment.”
If AI was used, you must specify:
- Tool(s) used (e.g., ChatGPT, GitHub Copilot, Claude, etc.)
- How they were used (e.g., explaining a concept, interpreting an error message, debugging code you wrote)
- Where they influenced your work (which questions/sections)
- Edits & verification you made (how you checked correctness, what you modified)
AI Use Template
- Used AI? Yes / No
- Tool(s):
- Purpose / How used (1–3 sentences):
- Scope (which questions/sections):
- Edits & verification (what you changed and how you checked correctness):
Example (for illustration only): - Used AI? Yes - Tools: ChatGPT (free) - How used: Asked what the
axisargument does in a pandas method, and for help debugging aKeyErrorwhen selecting columns. - Scope: 1.5, 2.2. - Edits & verification: Rewrote the selection in my own style; checked the result by comparing row counts before and after.
Grading (2 points): - 2 points = a clear statement that you did not use AI, or a complete, specific and honest disclosure (tools named, usage and verification described) - 1 point = a disclosure that is vague or missing required details - 0 points = no statement at all, or a disclosure that is misleading
Bonus: Clean the Survey Data (10 points, optional)
This is an optional question with no partial credit. You will get points only if your solution is completely correct. We advise you to attempt it only when you are done with the rest of the assignment.
The file data/stat303_survey.csv holds responses to a survey of STAT 303-1 students. In this question, we’ll work to clean this data a bit. As with every question, you are not allowed to use a for loop or any other loop.
The starter notebook supplies this code, which reads the data and cleans the column names:
survey_data = pd.read_csv("data/stat303_survey.csv")
new_col_names = ['parties_per_month', 'smoke', 'weed', 'introvert_extrovert', 'love_first_sight', 'learning_style', 'left_right_brained', 'personality_type', 'social_media', 'num_insta_followers', 'streaming_platforms', 'expected_marriage_age', 'expected_starting_salary', 'fav_sport', 'minutes_ex_per_week', 'sleep_hours_per_day', 'how_happy', 'farthest_distance_travelled', 'fav_number', 'fav_letter', 'internet_hours_per_day', 'only_child', 'birthdate_odd_even', 'birth_month', 'fav_season', 'living_location_on_campus', 'major', 'num_majors_minors', 'high_school_GPA', 'NU_GPA', 'age', 'height', 'height_father', 'height_mother', 'school_year', 'procrastinator', 'num_clubs', 'student_athlete', 'AP_stats', 'used_python_before', 'dominant_hand', 'childhood_in_US', 'gender', 'region_of_residence', 'political_affliation', 'cant_change_math_ability', 'can_change_math_ability', 'math_is_genetic', 'much_effort_is_lack_of_talent']
survey_data.columns = list(survey_data.columns[0:2])+new_col_namesCheck the datatype of the variables using the dtypes attribute of the Pandas DataFrame object. You will notice that only two variables are numeric. However, if you check the first few observations of the data with the function head() you will find the there are several more variables that seem to have numeric values.
Write a function that accepts a Pandas Series (or a column of a Pandas DataFrame object) as argument, and if the datatype of the Series is non-numeric, does the following:
Checks if at least 10 values of the Series contain a digit in them.
If at least 10 values are found to contain a digit, then:
A. Eliminate the characters
~,+, and,from all the values of the Series.B. Convert the Series to numeric (with coercion if needed).
If at least 10 values are NOT found to contain a digit, then:
A. If the values of the Series are ‘Yes’ and ‘No’, then replace ‘Yes’ with 1 and ‘No’ with 0. The Series datatype must change to numeric as well.
B. If the values of the Series are ‘Agree’ and ‘Disagree’, then replace ‘Agree’ with 1 and ‘Disagree’ with 0. The Series datatype must change to numeric as well.
Apply the function to each column of survey_data using the apply() method. Save the updated DataFrame as survey_data_clean. Then run the supplied check:
survey_data_clean.describe().loc['mean',:]It should print out the mean values of 28 numeric columns in the data survey_data_clean. The variables that you should see as numeric are given in the supplied list numeric_columns (this is just to check your work).
Note that your function must be general, i.e., it must work for any other dataset as well. This means, you cannot hard code a column name, or anything specific to survey_data in the function.
Prepare and Submit the HTML
Restart the kernel, run all cells in order, resolve unexpected errors, and save. Render from the assignment folder. The terminal starts in stat303-1, so first move it there with cd assignments and then cd assignment-c, and check the current directory. Then run:
quarto render assignment_c.ipynb --to html
Open assignment_c.html and check that your code, output, and written answers are visible. Upload it to Assignment C in Canvas.
This step is graded through the formatting points in instruction 8.