2014-08-29 73 views
0

我有一个CSV文件中包含这样的数据检索行:如何从一个CSV文件中使用代码字符串

我写下代码能够检索包含“激活”,在第二列的行“结果”:

数据:

No,Outcome,target,result 
1,Active,PGS2,positive 
2,inactive,IM2,negative 
3,inactive,IGI,positive 
4,Active,IIL,positive 
5,Active,P53,negative 

代码:

new_file = open(my_file) 
lines = new_file.readlines() 
for line in lines: 
    if "Active" in line: 
     print line, 

结果:

No,Outcome,target,result 
1,Active,PGS2,positive 
4,Active,IIL,positive 
5,Active,P53,negative 

我怎样才能使用熊猫库,这样我可以,如果我检索行后使用熊猫功能使代码更短写下这段代码。

此外,此代码不适合当您在“行”关键字相同的地方,因为它可以检索错误的行。预览了一些帖子后,我发现“pandas”非常适合CSV处理库。

回答

2

为什么不仅仅是过滤后,它会比逐行解析更快。只是这样做:

In [172]: 

df[df['Outcome']=='Active'] 
Out[172]: 
    No Outcome target result 
0 1 Active PGS2 positive 
3 4 Active IIL positive 
4 5 Active P53 negative 
+0

哎再次感谢你有没有喜欢pd.set_option任何函数(“display.max_rows”,无)”列也因为同样的问题,在我试图pd.set_option列的情况下产生的( 'display.max_columns',None)但它不起作用 – 2014-08-29 10:57:30

+0

您的意思是'max_colwidth'?在我之前给出的答案中有一个选项列表:http://stackoverflow.com/questions/21293536/list-of-pandas -options/21293562#21293562 – EdChum 2014-08-29 11:05:42

+0

再次感谢你“pd.set_option('display.line_width',2000)”为我工作 – 2014-08-29 11:29:50

相关问题