[英]Count how many row with last item is 1 or 0 in CSV file Python
I have CSV file like this: 我有这样的CSV文件:
2,1,2,3,1
23,3,2,22,0
2,2,11,2,0
1,2,2,1,1
.
.
44,3,3,44,0
2,2,11,2,0
Each row ended with 1 or 0, I want to count the prior probability for 1 or 0 by counting how many row with last item is 1 or 0 divide by total rows. 每行以1或0结尾,我想通过计算有最后一项是1或0的行数除以总行数来计算1或0的先验概率。 How to solve it in python. 如何在python中解决它。 Thank you. 谢谢。
import csv
total = 0
zeros = 0
ones = 0
with open('path/to/file') as infile:
for row in csv.reader(infile):
total += 1
if row[-1] == '0': zeros += 1
if row[-1] == '1': ones += 1
# do some division to calculate priors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.