大熊猫对groupby.sum的归一化

时间:2018-09-09 10:02:35

标签: pandas plot normalization pandas-groupby

我有一个熊猫数据框,如下所示:

      **I     SI     weights**
        1     3      0.3  
        2     4      0.2
        1     3      0.5
        1     5      0.5

我需要这样做:给定I值,考虑SI的每个值并加上总重量。最后,对于每个实现,我都应该有这样的东西:

             I = 1     SI = 3      weight = 0.8
                       SI = 5      weight = 0.5

             I = 2     SI = 4      weight = 0.2

这可以通过调用groupby和sum来轻松实现:

       name = ['I', 'SI','weight']
       Location = 'Simulationsdata/prova.csv'
       df = pd.read_csv(Location, names = name,sep='\t',encoding='latin1') 

       results = df.groupby(['I', 'real', 'SI']).weight.sum()

现在我希望将权重标准化为1,这样它应该是这样的:

             I = 1     SI = 3      weight = 0.615
                       SI = 5      weight = 0.385

             I = 2     SI = 4      weight = 1

我尝试过:

        for idx2, j in enumerate(results.index.get_level_values(1).unique()):
            norm = [float(i)/sum(results.loc[j]) for i in results.loc[j]]

但是当我尝试为每个I绘制SI的分布时,我发现SI也被标准化了,我不希望这种情况发生。

P.s。这个问题与this one有关,但是,由于它涉及问题的另一方面,因此我想最好单独提出一个问题

1 个答案:

答案 0 :(得分:1)

您应该可以将weight列除以其总和:

# example data
df
   I  SI   weight
0  1   3      0.3
1  2   4      0.2
2  1   3      0.5
3  1   5      0.5

# two-level groupby, with the result as a DataFrame instead of Series:
# df['col'] gives a Series, df[['col']] gives a DF
res = df.groupby(['I', 'SI'])[['weight']].sum()
res
       weight
I SI         
1 3       0.8
  5       0.5
2 4       0.2

# Get the sum of weights for each value of I,
# which will serve as denominators in normalization
denom = res.groupby('I')['weight'].sum()
denom
I
1    1.3
2    0.2
Name: weight, dtype: float64

# Divide each result value by its index-matched
# denominator value
res.weight = res.weight / denom
res
        weight
I SI          
1 3   0.615385
  5   0.384615
2 4   1.000000
相关问题