只需将两列相乘:
In [11]: df_city['weighted_jobs'] = df_city['weight'] * df_city['jobs']
现在您可以按城市分组(并取总和):
In [12]: df_city_sums = df_city.groupby('city').sum()In [13]: df_city_sumsOut[13]: jobs weight weighted_jobscity oakland 362 6907958san mateo 367 10179026sf 253 6386209[3 rows x 3 columns]现在,您可以将两个和除,以获得所需的结果:
In [14]: df_city_sums['weighted_jobs'] / df_city_sums['jobs']Out[14]: cityoakland 21.983425san mateo 24.594005sf24.541502dtype: float64



