我想通过根据特定列中的值选择行来限制要打印的行。
例如:
Column1, Column2, Column3
aaa, bbb, ccc
none, ddd, ggg
我只想打印Column1
值为none
的行。
这是我的代码:
for v in df:
if 'none' in df['2nd_prize']:
print v
答案 0 :(得分:1)
for row in table:
if row[0] is none:
print row
答案 1 :(得分:1)
您可以使用loc
对数据框的行进行子集,以限制"none"
中包含Column 1
的行,如下所示:
数据准备
In [1]: import pandas as pd
...: from io import StringIO
...:
In [2]: df = pd.read_csv(StringIO(
...: '''
...: Column1, Column2, Column3
...: aaa, bbb, ccc
...: none, ddd, ggg
...: kkk, jjj, ppp
...: none, eee, fff
...: '''))
<强>运营强>
In [3]: df.loc[df['Column1'] == "none"]
Out[3]:
Column1 Column2 Column3
1 none ddd ggg
3 none eee fff
答案 2 :(得分:1)
您可以使用带掩码的boolean indexing
:
import pandas as pd
df = pd.DataFrame({'Column2': {0: 'bbb', 1: 'ddd'},
'Column1': {0: 'aaa', 1: 'none'},
'Column3': {0: 'ccc', 1: 'ggg'}})
print (df)
Column1 Column2 Column3
0 aaa bbb ccc
1 none ddd ggg
print (df['Column1'] == "none")
0 False
1 True
Name: Column1, dtype: bool
print (df[df['Column1'] == "none"])
Column1 Column2 Column3
1 none ddd ggg
如果值包含字符串开头的空格,请使用str.strip
:
import pandas as pd
df = pd.DataFrame({'Column2': {0: ' bbb', 1: ' ddd'},
'Column1': {0: ' aaa', 1: ' none'},
'Column3': {0: ' ccc', 1: ' ggg'}})
print (df)
Column1 Column2 Column3
0 aaa bbb ccc
1 none ddd ggg
print (df['Column1'].str.strip() == "none")
0 False
1 True
Name: Column1, dtype: bool
print (df[df['Column1'].str.strip() == "none"])
Column1 Column2 Column3
1 none ddd ggg
答案 3 :(得分:1)
这是另一种方法,首先手动删除空白,然后将处理过的文件内容提供给pandas。
import pandas as pd
from io import StringIO
# First strip out the whitespace
contents = open('Input.txt').read().splitlines()
contents = "\n".join([",".join([x.strip() for x in y.split(",")]) for y in contents])
# Convert to a stringIO to feed to pandas
df = pd.read_csv(StringIO(unicode(contents, 'utf-8')))
print df[df['Column1'] == "none"]