【问题标题】:In Python, find average positive number and average negative number在 Python 中,求平均正数和平均负数
【发布时间】:2015-03-29 18:17:53
【问题描述】:

当数字为正数时,我想找到平均数,当数字为负数时,我想找到平均数。

import csv


totalCount = 0
numberOfPositives = 0

with open('Weather30States.csv', 'r') as file1:
     val = list(csv.reader(file1))[2]
     val1 = val[0:4]

with open('FL%.csv', 'r') as file2:
    reader = csv.reader(file2)
    reader.next() # this skips the first row of the file
    # this iteration will start from the second row of file2.csv
    conditionMet = False
    for row in reader:
        if conditionMet == True:
            if float(row[0].strip('%')) > 0: # change > to >= if you want to count 0 as positive
                print "FA, 1",row[0],',', ','.join(row[1:5]) # print 1 if positive
                numberOfPositives += 1 # add 1 to numberOfPositives only if positive
            else:
                print "FA, 0",row[0],',', ','.join(row[1:5]) # print 0 if not positive
            totalCount += 1 # add 1 to totalCount regardless of sign
            conditionMet = False # or break if you know you only need at most one line
        if row[1:5] == val1:
           conditionMet = True

print 'Total Count =', totalCount
print 'Percentage of Positive numbers =', numberOfPositives * 100./totalCount, '%'

我是 Python 和 Excel 的新手,不知道在哪里或如何做。

更新

我想要这部分的 row[0] 的平均值:

with open('FL%.csv', 'r') as file2:
    reader = csv.reader(file2)
    reader.next() # this skips the first row of the file
    # this iteration will start from the second row of file2.csv
    conditionMet = False
    for row in reader:
        if conditionMet == True:
            if float(row[0].strip('%')) > 0: # change > to >= if you want to count 0 as positive
                print "FA, 1",row[0],',', ','.join(row[1:5]) # print 1 if positive
                numberOfPositives += 1 # add 1 to numberOfPositives only if positive
            else:
                print "FA, 0",row[0],',', ','.join(row[1:5]) # print 0 if not positive
            totalCount += 1 # add 1 to totalCount regardless of sign
            conditionMet = False # or break if you know you only need at most one line
        if row[1:5] == val1:
           conditionMet = True

【问题讨论】:

  • 您的要求有点不清楚。您想要所有正行 [0] 值的平均值和所有负行 [0] 值的平均值吗?
  • 是的,汤姆

标签: python excel average


【解决方案1】:

您可以随时收集正值和负值,然后在最后取平均值。

from numpy import mean
neg_vals,pos_vals = [],[] #Two empty lists to add values into
#Your code goes here ... ... ... ... 
        if float(row[0].strip('%')) > 0: # change > to >= if you want to count 0 as positive
            print "FA, 1",row[0],',', ','.join(row[1:5]) # print 1 if positive
            numberOfPositives += 1 # add 1 to numberOfPositives only if positive
            pos_vals.append(float(row[0]))
        else:
            print "FA, 0",row[0],',', ','.join(row[1:5]) # print 0 if not positive
            neg_vals.append(float(row[0]))
#Rest of with open as file2
neg_mean = mean(neg_vals)
pos_mean = mean(pos_vals)

您可能必须格式化行[0](看起来您通常会从中删除“%”)。

此代码的工作原理是在列表出现时将正值和负值添加到列表中。在循环结束时,取两个列表的平均值。根据您希望代码的弹性程度,您可能需要包含一个测试用例,以检测无正值或无负值。

【讨论】:

  • 你好 Tom,我一直收到错误“TypeError: cannot perform reduce with flexible type”我认为它必须对 float 、 str 、array 做一些事情。你知道如何解决这个问题吗?
  • 哦,抱歉,row[0] 需要先设为浮点数。我编辑了答案代码以反映这一点。它现在将 float(row[0]) 附加到列表中,而不是 row[0],它不是浮点类型。
  • 你好汤姆,正确的方法是接近(我认为这里是菜鸟说话)是 pos_vals.append(float(row[0].strip('%'))) 你同意吗?
  • 很可能——我不知道你的数据是如何格式化的,所以我在代码后面加上了一个警告,即需要做一些事情来解释“%”。但这很可能是正确的方法。
  • Tom 非常感谢您的帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多