2017-08-11 64 views
0

我需要一些快速计算的帮助,在下面的分母线中我需要得到字符串出现的总和,但只需要求和高于某个值的值,因此对于例如,我需要得到所有这些的总和,但不包括附带的2一定次数的数量,所以理论上我需要沿着线的东西:在排除某些值的情况下确定总和

enominator = np.sum(occurances yet only sum above the value of occurances(2)) 



      # the next bit uses the True/False columns to find the ranges in which a 
      # series of avalanches happen. 
      fst = bins.index[bins['avalanche'] & ~ bins['avalanche'].shift(1).fillna(False)] 
      lst = bins.index[bins['avalanche'] & ~ bins['avalanche'].shift(-1).fillna(False)] 
      for i, j in zip(fst, lst): 
       bins.loc[j, 'total count'] = sum(bins.loc[i:j+1, 'count']) 
       bins.loc[j, 'total duration'] = (j-i+1)*bin_width 

      writer = pd.ExcelWriter(bin_file) 
      bins.to_excel(writer) 
      writer.save() 

      # When a series of avalanches occur, we need to add them up. 
      occurances = bins.groupby(bins['total count']).size() 

      # Fill in the gaps with zero 
      occurances = occurances.reindex(np.arange(occurances.index.min(), occurances.index.max()), fill_value=0) 
      # Create a new series that shows the percentage of outcomes 
      denominator = np.sum(occurances) 
      print(denominator) 
      percentage = occurances/denominator 
      #print (denomimator) 

所以,这需要一个excel文件并作为一个数据框运行它,但是,我遇到了麻烦,就像我之前提到的那样,计算可变分母。 Occurances只是增加了一个给定的值是本,但是,我需要计算分母的次数,使得:

分母= np.sum(occurances) - occurances [2] + occurances [1]

然而,如果发生[2]或发生[1]不存在,它会崩溃,那么我将如何去处理发生的总和[3]及以上,我也尝试过: 分母= np.sum(发生率)> =发生[3] 但它只给了我一个真和假的陈述,并会在不久之后崩溃。所以我基本上需要出现在[3]和以上的值的总和。感谢您的任何帮助表示赞赏

+2

你应该看看提供一个[MCVE],即简化上面的代码只是你的问题,所以它是别人提供的帮助更容易。掩盖的总和可能是你需要的,参见['numpy.ma.sum'](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.sum.html) – AChampion

回答

0

使用条件指数:

denominator = occurances[occurances > occurances(2)].sum() 
+0

我试过,但我得到的错误:TypeError:'系列'对象不可召唤 –

+0

什么错误?我假设'发生(2)'是不是有效的语法,但这只是从你的伪代码复制 - 你的意思是'发生[2]'? – AChampion

+0

感谢兄弟它的工作原理,但现在我得到了另一个令人讨厌的错误:在pandas._libs.hashtable.Float64HashTable.get_item(pandas \ _libs \ hashtable。)中的文件“pandas \ _libs \ hashtable_class_helper.pxi”,第339行。 c:7415) KeyError:3.0 –