TPM
TPM

Reputation: 894

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.

Upvotes: 3

Views: 2633

Answers (1)

TPM
TPM

Reputation: 894

@behzad.nouri gave me the solution:

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

does exactly what I want.

Upvotes: 6

Related Questions