283-7 Stratified Sampling, Another Alternative using df.unique()

Hello everyone,

I would like to share my solution for Stratified Sampling 283-7. Several other users had posted various solutions as well, and I’d like to add another possible solution for this matter using df.unique.

wnba['score_per_game'] = wnba['PTS']/wnba['Games Played']
mean_score_dict ={}
position_list = wnba['Pos'].unique()

for stratum in position_list:
    pos_strat = wnba[wnba['Pos'] == stratum]
    strat_sampling = pos_strat['score_per_game'].sample(10, random_state=0)
    mean_score_dict[stratum] = strat_sampling.mean()
position_most_points = max(mean_score_dict, key=mean_score_dict.get)

Hopefully this will help those who are stuck creating codes based on provided hint. I definitely got stuck.


1 Like