【发布时间】:2020-08-05 12:24:28
【问题描述】:
df 看起来像这样:
age population
0 20 2
1 21 3
2 22 2
3 23 5
4 24 7
df = pd.DataFrame({ 'age': [20, 21, 22, 23, 24], 'population': [2, 3, 2, 5, 7]})
我想计算总人口的中位年龄。有没有简单的方法来做到这一点?
得到这样的平均值,但我需要中位数:
df['years'] = df['age'] * df['population']
average_age= (df['years'].sum()/df['population'].sum())
【问题讨论】: