简体   繁体   中英

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" . 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM