Exercise

Calculating D in a Loop

Is Georgia's Index of Dissimilarity of of 0.544 high or low? Let's compare it to Illinois (FIPS = 17), home of Chicago.

In this exercise we will use a loop to calculate \(D\) for all states, then compare Georgia and Illinois.

Remember that the formula for the Index of Dissimilarity is:

$$D = \frac{1}{2}\sum{\left\lvert \frac{a}{A} - \frac{b}{B} \right\rvert}$$

pandas has been imported using the usual alias, and the tracts DataFrame with population columns "white" and "black" has been loaded. The variables w and b have been defined with the column names "white" and "black".

Instructions

100 XP
  • Use the unique() method on the "state" column to create a list of state FIPS codes.
  • Use a for-loop to store each element of states (that is, each FIPS code) in a variable named state.
  • Filter the tracts DataFrame on each value of state, and assign to tmp.
  • Calculate \(D\) for each state by applying the formula to tmp, and store the result in the dictionary state_D.