2016-09-23 37 views
1

我有以下数据框。获得加权平均然后分组在熊猫

 weight  x  value 
0   5  -8.7  2 
1   9  -8.7  3 
2   12 -21.4  10 
3   32 -21.4  15 

我需要得到值的加权平均值和分组在x上。结果将是:

-8.7:(5 /(5 + 9)* 2)+((9/14)* 3)= 2.64

-21.4:((12/44)×10) +((32/44)* 15)= 13.63

  x  weighted_value 
0  -8.7    2.64 
1 -21.4    13.63 
+0

你尝试过这么远吗?你有没有可以发布的任何代码,使我们更容易帮忙? – alexbclay

回答

1

numpy.average录取weights论点:

import io 
import numpy as np 
import pandas as pd 

data = io.StringIO('''\ 
     weight  x  value 
0   5  -8.7  2 
1   9  -8.7  3 
2   12 -21.4  10 
3   32 -21.4  15 
''') 
df = pd.read_csv(data, delim_whitespace=True) 

df.groupby('x').apply(lambda g: np.average(g['value'], weights=g['weight'])) 

输出:

x 
-21.4 13.636364 
-8.7  2.642857 
dtype: float64 
0

下面是一个使用NumPy的工具,量化的方法 -

# Get weighted averages and corresponding unique x's 
unq,ids = np.unique(df.x,return_inverse=True) 
weight_avg = np.bincount(ids,df.weight*df.value)/np.bincount(ids,df.weight) 

# Store into a dataframe 
df_out = pd.DataFrame(np.column_stack((unq,weight_avg)),columns=['x','wghts']) 

采样运行 -

In [97]: df 
Out[97]: 
    weight  x value 
0  5 -8.7  2 
1  9 -8.7  3 
2  12 -21.4  10 
3  32 -21.4  15 

In [98]: df_out 
Out[98]: 
     x  wghts 
0 -21.4 13.636364 
1 -8.7 2.642857