使用Python如何从.csv文件中获取'n'列中字符串的出现次数

时间:2015-06-09 11:25:36

标签: python-2.7 csv collections counter xls

I am new to python and my test report is a .csv file like this :

Stream play pause seek
1.mp3 PASS FAIL FAIL
1.ac3 PASS PASS FAIL
2.mp3 FAIL PASS PASS
3.mp3 PASS PASS FAIL
4.mp4 FAIL FAIL PASS

I want to get total number of "PASS" from all the columns.

Reslult应该是这样的: 总测试:15 通行证数量:8 失败者:7

请建议如何从“播放”,“暂停”和“搜索”列中提取字符串“PASS”。

1 个答案:

答案 0 :(得分:0)

我可以使用pandas模块做到这一点。     以下是代码:

import pandas as pd
pdread = pd.read_csv(csv_file)
for i in xrange(1,3,1):
    count = collections.Counter(pdread['play'])
    for (k,v) in collections.Counter(count).iteritems():
        #K here is either "PASS" or "FAIL"
        print k
        #v is count
        print v