【发布时间】:2015-05-25 07:59:32
【问题描述】:
我希望 python 计算一个负数和正数以二进制形式出现的次数 [1 个正数和 0 个负数]。此外,我希望 Python 计算总数中存在多少正数的百分比。在使用 Python Excel 时,我很难弄清楚这一点。
这是我现在拥有的代码:
import csv
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:
print "FA,",row[0],',', ','.join(row[1:5])
conditionMet = False # or break if you know you only need at most one line
if row[1:5] == val1:
conditionMet = True
当我运行这段代码时,我在输出窗口中得到的是:
FA, -1.97% , 0,0,1,0
FA, -0.07% , 0,0,1,1
FA, 0.45% , 0,1,1,1
FA, -0.07% , 0,0,1,1
FA, -0.28% , 0,0,1,1
我想得到的是这样的:
1, 0, FA, -1.97% , 0,0,1,0
2, 0, FA, -0.07% , 0,0,1,1
3, 1, FA, 0.45% , 0,1,1,1
4, 0, FA, -0.07% , 0,0,1,1
5, 0, FA, -0.28% , 0,0,1,1
Total Count = 5
Percentage of Positive numbers = .20 %
【问题讨论】:
-
您能提供每个文件的示例输入吗?对输入格式进行逆向工程以生成测试输出并非易事。
-
我已经为你添加了这两个文件
标签: python excel count frequency