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
print(midterm_grades)
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
print(midterm_grades)
G. Find the index location of 67 in midterm_grades
x = midterm_grades.index(67) #Locating the index of element 67
print(x)
H. Using a negative index, return 74 from midterm_grades
x = midterm_grades[-3] #Locating the third element going backward
print(x)
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
print (midterm_grades_array)
final_grades_array = np.array(final_grades) #Converting list to array
print (final_grades_array)
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
print(x)
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
print (midterm_grades_array)
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
print(x)
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"])
print (midterm_grades_series)
#Converting the array final_grades_array into a pandas series
final_grades_series = pd.Series(final_grades_array,index = ["Robert", "Mary", "Jane", "Frank"])
print (final_grades_series)
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
print(boolean_series)
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)
print(grades_df)
#Adding the series final_grades_series as a column to dataframe grades_df
grades_df ["final_grades"] = final_grades_series
print(grades_df)
B. Using .head() select the first 2 rows of data
grades_df.head(n=2)
C. Using .describe() get a summary of the data.
grades_df.describe(include = "all")
D. Select the first column of grades_df.
grades_df.iloc[:,0:1]
E. Select the first row in grades_df using .loc().
grades_df.loc["Robert"]
F. Select Frank's row of data using .iloc().
grades_df.iloc[3:4,:]