我有一个包含以下数据的CSV文件:
我已经写下了一个能够检索包含" Active"的行的代码。在第二栏"结果":
数据:
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库记下此代码,以便在检索行后使用pandas功能时可以缩短此代码。
此代码也不适用于你有" Active"关键字与yor行中的其他位置相同,因为它可以检索错误的行。我在预览了一些帖子后发现了" pandas"是非常适合CSV处理的库。
答案 0 :(得分: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