我有一个带有
的文本file1col0 col1
g1 text
g2 text,text
g3 text,text,text
g4 text
g5 text,text,text,text,text
需要使用pandas对其进行修改以删除所有带有多个文本输出的行,应该看起来像
col0 col1
g1 text
g4 text
唯一的差异我有约300,000行的文件
如果 col1
包含平弦:
In [94]: df
Out[94]:
col0 col1
0 g1 text
1 g2 text,text
2 g3 text,text,text
3 g4 text
4 g5 text,text,text,text,text
In [95]: df = df.loc[~df.col1.str.contains(',')]
In [96]: df
Out[96]:
col0 col1
0 g1 text
3 g4 text
In [105]: df
Out[105]:
col0 col1
0 g1 [text]
1 g2 [text, text]
2 g3 [text, text, text]
3 g4 [text]
4 g5 [text, text, text, text, text]
In [106]: df.col1.str.len() < 2
Out[106]:
0 True
1 False
2 False
3 True
4 False
Name: col1, dtype: bool
In [107]: df[df.col1.str.len() < 2]
Out[107]:
col0 col1
0 g1 [text]
3 g4 [text]
此答案基于 @maxu的概念,但这添加了一层概括,使您能够更改允许多少 text
值的条件。
df[df.col1.str.count(',') < 1]
col0 col1
0 g1 text
3 g4 text