简体   繁体   English

pandas的子集行dataframe上的字符串匹配

[英]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': pool是一个数据帧,其中一列是“Name”如果key =='Bob',那么这一行正确地给了我所有的行,其中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. 我反而希望获得与“Bob”匹配的所有行,例如“Bob Jones”和“Bob Marley”等。

So I changed '==' to 'in', but it doesn't work as I expected: 所以我将'=='更改为'in',但它不能像我预期的那样工作:

keyrows = pool[key in pool.Name]

I get KeyError: False 我得到KeyError:False

Any help would be much appreciated. 任何帮助将非常感激。

@behzad.nouri gave me the solution: @ behzad.nouri给了我解决方案:

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

does exactly what I want. 完全符合我的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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