在排除某些值时确定Sum

时间:2017-08-11 17:14:21

标签: python excel pandas sum

我需要一些快速计算的帮助,在下面的分母线中我需要得到字符串出现的总和,但只需要将值加在一个值之上,所以例如,我需要得到所有这些的总和,但排除了某个出现在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只是累加给定值的次数,但是,我需要计算分母,以便:

denominator = np.sum(occurrence) - 出现[2] +出现[1]

然而,如果它出现[2]或出现[1]并没有出现它崩溃,那么我将如何去获取出现的总和[3]及以上,我也尝试过: denominator = np.sum(occurrence)> =出现[3] 但它只给了我一个真实和错误的陈述,并且很快就会崩溃。所以我基本上需要出现[3]及以上的值的总和。谢谢你的任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

使用条件索引:

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