2017-03-16 42 views
1

我有一個數據系列,看起來像這樣更新數據框與分級指數

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 

我有,讓每年一定quantitiy矢量

 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 

我想打的divisoin我的數量在使用我的vector = 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並未改變。請提供任何提示。謝謝。

+0

你可以提供一個數據框,我們可以在Python複製,所以我們可以幫助您輕鬆。回答這個問題很簡單,但重新創建初始數據框需要時間,並且應該很容易爲您提供df = pd.DataFrame()可供您實例化的df。 –

+0

如何傳遞給你數據框?我正在使用的是發佈在問題上的那個。 – JPV

回答

1

我覺得你divNumber需要鴻溝,而是先轉換的dfindexyear S:

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 
+0

我正在按照你提及的方式獲取NaN號碼: df2.ix ['PS'] = df2.ix ['PS']。div(df3.Number,level = 1) – JPV

+0

還有一些問題? – jezrael

+0

我修好了!因爲我們在除法函數中使用了level = 1選項,所以不需要df2.ix ['PS']。非常感謝!! – JPV