简体   繁体   中英

In Python, find average positive number and average negative number

I would like to find what the average number is when the number is positive and I would like to find the average number when the number is negative.

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, '%'

I am new to Python and Excel that I have no idea where or how to do it.

Update

I want the avg from row[0] from this part:

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

You can just collect your positive and negative values as you go, then average them at the end.

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)

You may have to format the row[0] (it looks like you would normally strip the '%' from it).

This code works by just adding positive and negative values into lists as they appear. At the end of the loop, you take the average of the two lists. Depending on how resilient you want your code to be, you may need to include a test case for no positive or no negative values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM