使用分层索引更新数据框

Update dataframe with hierarchical index

我有一个看起来像这样的数据系列

Component  Date  Sev         Counts
PS         2009  3            4
                 4            1
           2010  1            2
                 3            2
                 4            1
           2011  2            3
                 3            5
                 4            1
           2012  1            1
                 2            5
                 3            7
           2013  2            4
                 3            9
           2014  1            2
                 2            3
                 3            4
           2015  1            2
                 2          100
                 3           31
                 4           31
           2016  1           44
                 2           27
                 3           45
Name: Alarm Name, dtype: int64

我有一个向量,每年给出一定数量

        Number
Date    
2009-12-31  8.0
2010-12-31  3.0
2011-12-31  13.0
2012-12-31  2.0
2013-12-31  3.0
2014-12-31  4.0
2015-12-31  6.0
2016-12-31  71.0

我想用我的向量 = Counts/number 的除法对系列中的计数进行除法。我还想获得带有更新数字的原始数据框。

这是我的代码

count=0
for i in df3.index.year:

    df2.ix['PS'].ix[i].apply(lambda x:  x /float(df3.iloc[count]))
    count = count + 1 

但是我的数据框 df2 没有改变。请任何提示。谢谢。

我想你需要除以 div column Number, but first convert index of df to years:

df.index = df.index.year
s = s.div(df.Number, level=1)
print (s)

Component  Date  Sev Counts
PS         2009  3              0.500000
                 4              0.125000
           2010  1              0.666667
                 3              0.666667
                 4              0.333333
           2011  2              0.230769
                 3              0.384615
                 4              0.076923
           2012  1              0.500000
                 2              2.500000
                 3              3.500000
           2013  2              1.333333
                 3              3.000000
           2014  1              0.500000
                 2              0.750000
                 3              1.000000
           2015  1              0.333333
                 2             16.666667
                 3              5.166667
                 4              5.166667
           2016  1              0.619718
                 2              0.380282
                 3              0.633803
dtype: float64