58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
#! /usr/bin/env
|
|
|
|
import pandas as pd
|
|
import math
|
|
|
|
# helper to calculate an overall district rating
|
|
def get_overall_rating(df: pd.DataFrame):
|
|
'''
|
|
Returns a DataFrame that contains the overall district scores.
|
|
Parameters:
|
|
df (DataFrame):
|
|
Returns:
|
|
overall (DataFrame):
|
|
'''
|
|
weights = {
|
|
'e': 0.3,
|
|
'm': 0.2,
|
|
'h': 0.5
|
|
}
|
|
levels = weights.keys()
|
|
empty_stats = {
|
|
'e_ave': 0,
|
|
'm_ave': 0,
|
|
'h_ave': 0,
|
|
'weighted_ave': 0
|
|
}
|
|
overall = pd.DataFrame()
|
|
|
|
districts = list(df.index.unique(level=0))
|
|
for district in districts:
|
|
scores = {}
|
|
stats = empty_stats
|
|
weighted_ave = 0
|
|
schools = list(df.loc[district].index.unique(level=0))
|
|
if len(schools) <= 1:
|
|
continue
|
|
for level in levels:
|
|
scores[level] = []
|
|
for school in schools:
|
|
this_school = df.loc[[(district,school)]]
|
|
codes = this_school['level-codes'].iloc[0]
|
|
if level in codes:
|
|
rating = float(this_school['rating'].iloc[0])
|
|
if not math.isnan(rating):
|
|
scores[level].append(this_school['rating'].iloc[0])
|
|
#print(district, scores)
|
|
try:
|
|
stats[level+'_ave'] = round(sum(scores[level])/len(scores[level]),2)
|
|
except:
|
|
continue
|
|
for weight in weights.keys():
|
|
weighted_ave += weights[weight]*stats[weight+'_ave']
|
|
stats['weighted_ave'] = round(weighted_ave,2)
|
|
district_dict = {'district-name': district}
|
|
district_dict = {**district_dict, **stats}
|
|
overall = pd.DataFrame(district_dict)
|
|
print(overall)
|
|
return |