Question: Task 2 7 Let's make a smaller dataframe to use while we are working out the larger problem. Create a dataframe called 'schools _ subset'

Task 27
Let's make a smaller dataframe to use while we are working out the larger problem.
Create a dataframe called 'schools_subset' from 'schools' that includes only Grade 3 for the year 2012. We are not going to need all the columns, so only add district, grade, year, and mean scale score to the new dataframe.
Display the schools_subset dataframe.
Task 28
Look up the DataFrame rank method. We will use this method to set the ranks. Since we want the highest rank to be 1, we need to set the 'ascending' parameter to the rank call to False.
Make a function called add_default_rank that takes a dataframe the first parameter and a 'column' parameter with the default 'Mean Scale Score' as the second paramter.
This function should create a new column called 'Default Rank' in the passed dataframe. The value of the new column should be the rank for the passed 'column' parameter.
Demonstrate your add_default_rank function works by calling the function on the 'students_subset' dataframe. Print the 'schools_subset' dataframe before and after the invocation of the function to confirm that (1) the 'Default Rank' column was added and (2) that the rank assigned to each row is correct based upon the 'Mean Scaled Score' value.
Task 29
Now that you have the add_default_rank function that works on a dataframe, you can use the apply method to apply that method to each group from a groupby.
Group the full 'schools' dataframe by grade and year, then apply add_default_rank to the groups. Store the results of this into a new 'schools2' dataframe.
Display the resulting 'schools2' dataframe.
Task 30
The 'schools2' now has a rank for each district for each grade and each year.
To fulfill the Mayor's ranking request, we can now produce the ordered list of districts with the top performers at the top of the list.
To do this, sum the 'Default Rank' column grouping by District and sorting the result using the sort_values method with "ascending" set to True and show the results.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!