简体   繁体   中英

How to compute weighted average

Country  life_expectancy   population 

Germany     70               3000000
France      75               450000
USA         70               350000
India       65               4000000
Pakistan    60               560000
Belgium     68               230000

I want to calculate the weighted average life expectancy according to the formula below:

∑ (𝑙𝑖𝑓𝑒𝑖 × 𝑝𝑜𝑝𝑖)/ ∑ 𝑝𝑜𝑝𝑖  

where 𝑙𝑖𝑓𝑒𝑖 = life expectancy
      𝑝𝑜𝑝𝑖 = population

NOTE: The weighted average life expectancy is computed with the sum of the products of life expectancy by the total population of each country divided by the sum of the total population of each country

Can anyone please tell me how to solve this using for loop?

Using numpy.average(..., weights=...) :

Ref: https://docs.scipy.org/doc/numpy/reference/generated/numpy.average.html

import numpy as np

res=np.average(df["life_expectancy"], weights=df["population"])

Outputs:

67.22817229336438

with a for loop

numerator, denominator = 0, 0
for i in df.index:
    numerator += df.loc[i, 'life_expectancy'] * df.loc[i, 'population']
    denominator += df.loc[i, 'population']
weighted_average = numerator / denominator

or using pandas to do everything faster and in any easier to read way (this is my recommended solution)

weighted_average = (df['life_expectancy']*df['population']).sum() / df['population'].sum()

其实这里不需要for循环,可以直接计算

life_exp = (countries_df.life_expectancy*countries_df.population).sum()/countries_df.population.sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM