Hello everyone,
I think the answer to this instruction is somehow complex to understand, and it can be answered in a shorter yet more understandable way. Here is my solution:
Screen Link: https://app.dataquest.io/m/283/sampling/7/stratified-sampling
My Code:
wnba['SPG'] = wnba['PTS'] / wnba['Games Played']
mean_pos = dict()
for position in wnba['Pos'].unique():
mean_score = wnba.groupby('Pos').get_group(position)['SPG'].sample(10, random_state=0).mean()
mean_pos[position] = mean_score
position_most_points = max(mean_pos, key=mean_pos.get)
I hope my solutions help my fellow learners to understand this part easier.
Enjoy Learning!
13 Likes
ashkan.ghanavati92:
for position in wnba['Pos'].unique():
mean_score = wnba.groupby('Pos').get_group(position)['SPG'].sample(10, random_state=0).mean()
mean_pos[position] = mean_score
position_most_points = max(mean_pos, key=mean_pos.get)
This makes more sense to me, I hate writing out multiple variable names per group if I can avoid it. Way tidier.
3 Likes
Great answers!
I also had something simpler
wnba["PPG"] = wnba["PTS"] / wnba["Games Played"]
strata = ['F','G','C','G/F','F/C']
stratum_dict = {}
for s in strata:
sample = wnba[wnba['Pos'] == s].sample(10, random_state=0)
mean = sample["PPG"].mean()
stratum_dict[s] = mean
position_most_points = max(stratum_dict,key=stratum_dict.get)
For the first time in a month it’s really clear what I’m doing haha.
9 Likes