![](/img/trans.png)
[英]How to count the number of occurences before a particular value in dataframe python?
[英]python how to count number of occurences which were extracted from another input file
#!/usr/bin/python
import os
import re
path=os.getcwd()
list_dir=os.listdir(path+'//Osativa')
if not os.path.exists('results'):
os.makedirs('results')
fo1=open('./results/annotation.txt','w')
f1=open(path+'//CLEAN FILE.csv').readlines()
f2=open(path+'//Osativa//Osativa_323_v7.0.annotation_info.txt').readlines()
for line in f1:
locus_id=line.split(',')[1]
if locus_id.startswith('LOC'):
print locus_id
我的問題是如何計算使用上述腳本提取的locus_id的數量。
您可以對它們進行計數,也可以將它們存儲在容器中。
數數
num = 0
for line in f1:
locus_id=line.split(',')[1]
if locus_id.startswith('LOC'):
num += 1
print(num)
容器
ids = []
for line in f1:
locus_id=line.split(',')[1]
if locus_id.startswith('LOC'):
ids.append(locus_id)
print(len(ids))
您可以使用csv
模塊!
讀取文件后,遍歷每一行,獲取第一項( line[1]
),然后檢查是否存在“ LOC”。 他們將結果列表加總!
import csv
f1=open(path+'//CLEAN FILE.csv')
reader = csv.reader(f1)
print sum(1 for line in reader if 'LOC' in line[1])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.