Reputation: 5922
I want to keep only rows in a dataframe that contains specific text in column "col"
. In this example either "WORD1"
or "WORD2"
.
df = df["col"].str.contains("WORD1|WORD2")
df.to_csv("write.csv")
This returns True
or False
. But how do I make it write entire rows that match these critera, not just present the boolean?
Upvotes: 2
Views: 6277
Reputation: 393973
What is returned is a boolean series you use that to filter the 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
Upvotes: 5