# The optional life expectancy question

This is my solution but i am not sure if i am using the right formula. the instruction say use population as weight but i did not use weight here.
will my calculation be right or wrong. Below is my formula and my answer is
72.47942857142857

`life_exp = countries_df[“life_expectancy”].sum() / len(countries_df[“life_expectancy”])

This is not weighted average at all.

First you need to introduce weights for each of the country. You can do this by dividing the “population” column by the total population. You’ll get an array that represents the weights of each country, based on it’s population.

Then you multiply those weights by life expectancy of each country (basically you take the product of “life_expectancy” column and the weights).

Now you just have to sum the results to achieve weighted average.

### Reasoning

In usual average you multiply each of the data point by 1/n, where n is the number of such points. You can call this 1/n value a weight, that’s sort of universal for each data point. Summing the weights would give you a value of 1.

In weighted average, you also multiply each data point, but the weight depends on “importance” of such data point. Again, the weights sum up to 1 (you can check that when you introduce the weights array).

Then, for both cases, you just sum up those scaled values to achieve an average.

Equations:

1 Like