Python Pandas For Your Grandpa - 2.13 Challenge: Fair Teams
Contents
Setup
You’re organizing a competitive rock-skipping league. 6 coaches and 20 players have signed up. Your job is to randomly and fairly determine the teams, assigning players to coaches. Keep in mind that some teams will have three players and some teams will have four players. Given a Series of coaches
and a Series of players
, create a Series of random coach-to-player mappings. The resulting Series should have coach names in its index and corresponding player names in its values.
import numpy as np
import pandas as pd
coaches = pd.Series(['Aaron', 'Donald', 'Joshua', 'Peter', 'Scott', 'Stephen'], dtype='string')
print(coaches)
## 0 Aaron
## 1 Donald
## 2 Joshua
## 3 Peter
## 4 Scott
## 5 Stephen
## dtype: string
players = pd.Series(['Asher', 'Connor', 'Elizabeth', 'Emily', 'Ethan', 'Hannah', 'Isabella', 'Isaiah', 'James',
'Joshua', 'Julian', 'Layla', 'Leo', 'Madison', 'Mia', 'Oliver', 'Ryan', 'Scarlett', 'William',
'Wyatt'], dtype='string')
print(players)
## 0 Asher
## 1 Connor
## 2 Elizabeth
## 3 Emily
## 4 Ethan
## 5 Hannah
## 6 Isabella
## 7 Isaiah
## 8 James
## 9 Joshua
## 10 Julian
## 11 Layla
## 12 Leo
## 13 Madison
## 14 Mia
## 15 Oliver
## 16 Ryan
## 17 Scarlett
## 18 William
## 19 Wyatt
## dtype: string
Solution
coaches = coaches.sample(frac=1, random_state=2357)
players = players.sample(frac=1, random_state=7532)
repeats = np.ceil(len(players)/len(coaches)).astype('int64')
coaches_repeated = pd.concat([coaches] * repeats).head(len(players))
result = players.copy()
result.index = pd.Index(coaches_repeated, name='coach')
print(result)
## coach
## Stephen Julian
## Scott Joshua
## Aaron Elizabeth
## Joshua Asher
## Peter Oliver
## Donald William
## Stephen Wyatt
## Scott Isaiah
## Aaron Ethan
## Joshua Madison
## Peter Leo
## Donald Ryan
## Stephen Scarlett
## Scott Mia
## Aaron Connor
## Joshua James
## Peter Emily
## Donald Hannah
## Stephen Layla
## Scott Isabella
## dtype: string
Course Curriculum
- Introduction
1.1 Introduction - Series
2.1 Series Creation
2.2 Series Basic Indexing
2.3 Series Basic Operations
2.4 Series Boolean Indexing
2.5 Series Missing Values
2.6 Series Vectorization
2.7 Seriesapply()
2.8 Series View vs Copy
2.9 Challenge: Baby Names
2.10 Challenge: Bees Knees
2.11 Challenge: Car Shopping
2.12 Challenge: Price Gouging
2.13 Challenge: Fair Teams - DataFrame
3.1 DataFrame Creation
3.2 DataFrame To And From CSV
3.3 DataFrame Basic Indexing
3.4 DataFrame Basic Operations
3.5 DataFrameapply()
3.6 DataFrame View vs Copy
3.7 DataFramemerge()
3.8 DataFrame Aggregation
3.9 DataFramegroupby()
3.10 Challenge: Hobbies
3.11 Challenge: Party Time
3.12 Challenge: Vending Machines
3.13 Challenge: Cradle Robbers
3.14 Challenge: Pot Holes - Advanced
4.1 Strings
4.2 Dates And Times
4.3 Categoricals
4.4 MultiIndex
4.5 DataFrame Reshaping
4.6 Challenge: Class Transitions
4.7 Challenge: Rose Thorn
4.8 Challenge: Product Volumes
4.9 Challenge: Session Groups
4.10 Challenge: OB-GYM - Final Boss
5.1 Challenge: COVID Tracing
5.2 Challenge: Pickle
5.3 Challenge: TV Commercials
5.4 Challenge: Family IQ
5.5 Challenge: Concerts