Index Alignment Exercises

To illustrate how index alignment can sometimes lead to problems, let’s consider the following example: suppose we’re throwing a party, and we plan to give people prizes based on the order in which they arrive. The first person to arrive at the party will get 20 dollars, the second will get 10 dollars, and the third person doesn’t get anything.

To keep track of how many prizes everyone gets, we build a DataFrame with all the party attendees, their arrival order, and a column for keeping track of how much they’ve received in prizes.

The we can also build a Series with the prize amounts we plan to give people.

Exercise 1

Use the code below to get started:

import pandas as pd
attendees = pd.Series({'names': ["Jill", "Kumar", "Zaira"],
                          'prizes': [0, 0, 0],
                          'arrival_order': [2, 1, 3]})
arrival_prizes = pd.Series([20, 10, 0])

Exercise 2

Now let’s sort our attendees list by arrival_order so that the first row is the person who arrived first, the second is the person who arrived second, etc. to match how we’ve organized arrival_prizes.

Exercise 3

Now let’s “give” everyone their arrival prizes by adding arrival prizes to people’s prize column:

attendees['prizes'] = attendees['prizes'] + arrival_prizes

Exercise 4

Now let’s look at the result. Does it look like what you expected? Do you know what went wrong?

After you’ve formulated your thoughts, continue to Discussion.