简体   繁体   中英

subset rows of pandas dataframe by string match on column

pool is a dataframe, and one of the columns is "Name" If key == 'Bob', then this line correctly gives me all the rows where Name=='Bob':

keyrows = pool[key == pool.Name]

I instead want to get all the rows that match 'Bob', like "Bob Jones" and "Bob Marley", etc.

So I changed '==' to 'in', but it doesn't work as I expected:

keyrows = pool[key in pool.Name]

I get KeyError: False

Any help would be much appreciated.

@behzad.nouri gave me the solution:

keyrows = pool[pool.Name.str.contains(key)]

does exactly what I want.

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