python判断正数还是负数_PythonExcel如何知道出现负数和正数的次数(计数/频率)?...

使用两个计数器变量来跟踪总计数和阳性数。在开始时将它们设置为0,然后无论何时您想要添加1,在循环中使用+= 1。

然后,通过去掉百分比符号,然后使用以下方法将字符串转换为数字,测试百分比是否大于0。if float(row[0].strip('%')) > 0。你可以将此更改为>=如果您想将0包含在“正”类别中。totalCount = 0

numberOfPositives = 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

然后,你可以计算你所需的总和百分比。totalCount和numberOfPositives:print 'Total Count =', totalCount

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值