LoslegenKostenlos loslegen

Create Function to Calculate D

Calculating the Index of Dissimilarity requires multiple steps and has high reuse potential. In this exercise you will create the function dissimilarity that we used in the previous exercise. The function's input parameters will be a DataFrame of small area geographies (such as tracts) and three column names: the two columns with population counts of Group A and Group B, and the column with the names or geographic identifiers of the container geography (such as states or metro areas).

As a reminder, the formula the 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. The groupby and merge are already completed for you in the code below.

Diese Übung ist Teil des Kurses

Analyzing US Census Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Calculate the expression inside the absolute value bars based on the formula: The column names for \(A\) and \(B\) are formed by adding the suffix "_sum" to the parameters col_A and col_B
  • The sum method on a single column returns a series; use the to_frame() method to convert the series to a DataFrame
  • Test the new function on tracts: calculate White-Black dissimilarity by MSA name

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

def dissimilarity(df, col_A, col_B, group_by):

    # Sum Group A and Group B by grouping column
    grouped_sums = df.groupby(group_by)[[col_A, col_B]].sum()
    tmp = pd.merge(df, grouped_sums, left_on = group_by, 
                   right_index = True, suffixes = ("", "_sum"))
    
    # Calculate inner expression
    tmp["D"] = abs(____)
    
    # Calculate Index of Dissimilarity and convert to DataFrame
    return 0.5 * tmp.groupby(group_by)["D"].sum().____
  
msa_D = dissimilarity(msa_tracts, ____, ____, "msa_name")
print(msa_D.head())
Code bearbeiten und ausführen