[英]How to access the second to last row of a csv file using python Pandas?
[英]Count how many row with last item is 1 or 0 in CSV file Python
我有這樣的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
每行以1或0結尾,我想通過計算有最后一項是1或0的行數除以總行數來計算1或0的先驗概率。 如何在python中解決它。 謝謝。
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.