Contents

Python Pandas For Your Grandpa - 4.7 Challenge: Rose Thorn

Setup

You developed a multi-player indie game called “Rose Thorn”. Players compete in one of two venues - the ocean or the desert. You track the outcome of five games between three players in a DataFrame called games, and now you want to analyze the data. Convert the games DataFrame into a new DataFrame that identifies how many times each (player, placement) occurs per venue, specifically with venue as the row index and (player, placed) as the column MultiIndex.

import numpy as np
import pandas as pd

games = pd.DataFrame({
    'bella1':   ['2nd', '3rd', '1st', '2nd', '3rd'],
    'billybob': ['1st', '2nd', '2nd', '1st', '2nd'],
    'nosoup4u': ['3rd', '1st', '3rd', '3rd', '3rd'],
    'venue': ['desert', 'ocean', 'desert', 'ocean', 'desert']
})
print(games)
##   bella1 billybob nosoup4u   venue
## 0    2nd      1st      3rd  desert
## 1    3rd      2nd      1st   ocean
## 2    1st      2nd      3rd  desert
## 3    2nd      1st      3rd   ocean
## 4    3rd      2nd      3rd  desert

Solution

step1 = games.melt(id_vars='venue', var_name='player', value_name='placed')
step1['venue'] = pd.Categorical(step1.venue)
step1['player'] = pd.Categorical(step1.player)
step1['placed'] = pd.Categorical(step1.placed)
step2 = step1.groupby(['venue', 'player', 'placed']).agg('size')
step3 = step2.unstack(level=['player','placed'])
print(step3)
## player bella1         billybob         nosoup4u        
## placed    1st 2nd 3rd      1st 2nd 3rd      1st 2nd 3rd
## venue                                                  
## desert      1   1   1        1   2   0        0   0   3
## ocean       0   1   1        1   1   0        1   0   1

Course Curriculum

  1. Introduction
    1.1 Introduction
  2. 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 Series apply()
    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
  3. DataFrame
    3.1 DataFrame Creation
    3.2 DataFrame To And From CSV
    3.3 DataFrame Basic Indexing
    3.4 DataFrame Basic Operations
    3.5 DataFrame apply()
    3.6 DataFrame View vs Copy
    3.7 DataFrame merge()
    3.8 DataFrame Aggregation
    3.9 DataFrame groupby()
    3.10 Challenge: Hobbies
    3.11 Challenge: Party Time
    3.12 Challenge: Vending Machines
    3.13 Challenge: Cradle Robbers
    3.14 Challenge: Pot Holes
  4. 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
  5. 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

Additional Content

  1. Python NumPy For Your Grandma
  2. Neural Networks For Your Dog
  3. Introduction To Google Colab