[英]Pandas select and write rows that contain certain text
I want to keep only rows in a dataframe that contains specific text in column "col"
. 我只想在包含特定列"col"
文本的数据框中保留行。 In this example either "WORD1"
or "WORD2"
. 在此示例中, "WORD1"
或"WORD2"
。
df = df["col"].str.contains("WORD1|WORD2")
df.to_csv("write.csv")
This returns True
or False
. 这将返回True
或False
。 But how do I make it write entire rows that match these critera, not just present the boolean? 但是,如何使它写入与这些critera匹配的整个行,而不仅仅是呈现布尔值?
What is returned is a boolean series you use that to filter the df: 返回的是一个布尔系列,您可以使用它来过滤df:
df = df[df["col"].str.contains("WORD1|WORD2")]
You can then write this out as normal: 然后,您可以照常将其写出:
df.to_csv("write.csv")
Example: 例:
In [14]:
df = pd.DataFrame({'col':['word', 'WORD1', 'WORD2', 'WORD3']})
df
Out[14]:
col
0 word
1 WORD1
2 WORD2
3 WORD3
In [15]:
df[df['col'].str.contains('WORD1|WORD2')]
Out[15]:
col
1 WORD1
2 WORD2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.