dimanche 29 mars 2015

python excel find average positive number and average negative number

I am need of some help. 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 so new to python and excel that I have no idea where or how to do it. Thank you


Aucun commentaire:

Enregistrer un commentaire