python - Pandas select and write rows that contain certain text -


i want keep rows in dataframe contains specific text in column "col". in example either "word1" or "word2".

df = df["col"].str.contains("word1|word2") df.to_csv("write.csv") 

this returns true or false. how make write entire rows match these critera, not present boolean?

what returned boolean series use filter df:

df = df[df["col"].str.contains("word1|word2")] 

you can write out 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 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -