This homework is Due Midnight 11/15
There is a file upload activity on Engage where you can submit your HW.
This Homework is out of 100 points. Each question counts for 5 points.
A. Create a variable named x and write a Boolean Expression that evaluates to True if the variable is 697.
x = 697 #Assigned variable x an integer value of 697
x == 697 #Boolean comparison between variable x and the integer 697
B. Create a variable named x and write a Boolean Expression that evaluates to True if the variable is "not" 99.
x = 100 #Assigned variable x an integer value of 100
x != 99 #Boolean comparison between variable x and the integer 99
C. Create a variable named x and write a Boolean Expression that evaluates to True if the variable is greater than 4 or less than -7.
x = 5 #Assigned variable x an integer value of 5
(x > 4) | (x < -7) #Boolean and logical comparison
D. Create a variable named x and write a Boolean Expression that evaluates to True if the variable is less than 4 and greater than -7.
x = -5 #Assigned variable x an integer value of -5
(x < 4) & (x > -7) #Boolean and logical comparison
Use the list below for questions E to H
#DO NOT CHANGE CODE IN THIS CELL #Make sure to run this cell so the variable midterm_grades is saved midterm_grades = [ 74, 67]
E. Add a grade of 94 to the end of the list
midterm_grades.append(94) #Appended the integer 94 to the end of the array
[74, 67, 94]
F. Add a grade of 80 to the front of the list
midterm_grades.insert(0, 80) #Inserted the integer 80 at the beginning of the array
[80, 74, 67, 94]
G. Find the index location of 67 in midterm_grades
x = midterm_grades.index(67) #Locating the index of element 67
H. Using a negative index, return 74 from midterm_grades
x = midterm_grades[-3] #Locating the third element going backward
Use the list below for this section in addition to midterm_grades
#DO NOT CHANGE CODE IN THIS CELL #Make sure to run this cell so the variable final_grades is saved final_grades = [90, 84, 70, 65]
A. Import the pandas and numpy libraries using the coding format outlined in class.
import pandas as pd import numpy as np
B. Convert midterm_grades and final_grades into numPy arrays (You should have 2 seperate arrays).
Name these arrays midterm_grades_array and final_grades_array.
midterm_grades_array = np.array(midterm_grades) #Converting list to array
[80 74 67 94]
final_grades_array = np.array(final_grades) #Converting list to array
[90 84 70 65]
C. Using the np.average() function, find the average score for midterm grades in the class. (Put the array varaible between the parenthesis).
x = np.average(midterm_grades_array) #Calculating the average of midterm grades
D. You are giving all your students 2 extra credit points on their midterm grade. Make the necessary changes to midterm_grades_array.
Note: Make sure to save the changes to the midterm_grades_array variable.
midterm_grades_array = midterm_grades_array + 2 #Add 2 to each midterm grade
[82 76 69 96]
E. Use the np.average() function again to get the class average. It should have increased by 2 points.
x = np.average(midterm_grades_array) #Calculating the average of the new midterm grades
F. You want to add student names to midterm_grades. Convert the array into a series and add name labels to each grade.
The students names in order are Robert, Mary, Jane, and Frank.
Name these variables as midterm_grades_series and final_grades_series.
#Converting the array midterm_grades_array into a pandas series
midterm_grades_series = pd.Series(midterm_grades_array,index = ["Robert","Mary","Jane","Frank"])
Robert 82 Mary 76 Jane 69 Frank 96 dtype: int32
#Converting the array final_grades_array into a pandas series
final_grades_series = pd.Series(final_grades_array,index = ["Robert", "Mary", "Jane", "Frank"])
Robert 90 Mary 84 Jane 70 Frank 65 dtype: int32
G. Use a Boolean Operator see which student had a higher grade on their midterm than their final.
This should result in a boolean series.
boolean_series = midterm_grades_series > final_grades_series
Robert False Mary False Jane False Frank True dtype: bool
You can easily convert a pandas series into a pandas dataframe.
Look at the code below and notice that you can use the DataFrame function with your series to make a dataframe.
Since series are only one dimensional you must specify the name of the first column otherwise it will be labelled as 0.
Running this code should result in 1 column labelled midterm_grades and rows labelled with student names.
#DO NOT CHANGE CODE IN THIS CELL #Make sure to run this cell so the dataframe grades is saved grades_df = pd.DataFrame(midterm_grades_series , columns = ['midterm_grades'] )
A. Add a column to the grades_df variable named final_grades and set the data elements in that column equal to final_grades_series.
(You are adding the final grades data to the dataframe)
midterm_grades Robert 82 Mary 76 Jane 69 Frank 96
#Adding the series final_grades_series as a column to dataframe grades_df
grades_df ["final_grades"] = final_grades_series
midterm_grades final_grades Robert 82 90 Mary 76 84 Jane 69 70 Frank 96 65
B. Using .head() select the first 2 rows of data
C. Using .describe() get a summary of the data.
grades_df.describe(include = "all")
D. Select the first column of grades_df.
E. Select the first row in grades_df using .loc().
midterm_grades 82 final_grades 90 Name: Robert, dtype: int32
F. Select Frank's row of data using .iloc().