LoslegenKostenlos loslegen

Determine the average age by population

As Jason discussed in the video, to calculate a weighted average, we first find the total sum of weights multiplied by the values we're averaging, then divide by the sum of all the weights.

For example, if we wanted to find a weighted average of data = [10, 30, 50] weighted by weights = [2,4,6], we would compute \(\frac{2 \cdot 10 + 4 \cdot 30 + 6 \cdot 50}{2+4+6}\), or sum(weights * data) / sum(weights).

In this exercise, however, you will make use of func.sum() together with select to select the weighted average of a column from a table. You will still work with the census data, and you will compute the average of age weighted by state population in the year 2000, and then group this weighted average by sex.

Diese Übung ist Teil des Kurses

Introduction to Databases in Python

Kurs anzeigen

Interaktive Übung

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

# Import select and func
____

# Select the average of age weighted by pop2000
stmt = select([func.sum(____ * ____) / ____
			  ])
Code bearbeiten und ausführen