pythonnumpypandas
Ben Gorman

Ben Gorman

Life's a garden. Dig it.

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')
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(coaches)
# 0      Aaron
# 1     Donald
# 2     Joshua
# 3      Peter
# 4      Scott
# 5    Stephen
# 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

This content is gated

Subscribe to one of the products below to gain access