[英]Search for string in one column and extract all the column for matched row Python
I am trying to search for a string in one column and I want to extract whole dataframe when string matches in the column我正在尝试在一列中搜索一个字符串,当该列中的字符串匹配时,我想提取整个数据框
My Data is as follows我的数据如下
**string** **Number**
hello there 11
Hello hi 22
How are you 33
My code is as follows我的代码如下
string_hello = []
for i in df['string']:
if re.search("Hello",i, flags=re.I):
string_hello.append(i)
Expected result is预期结果是
**string** **Number**
hello there 11
Hello hi 22
But My code only gives string column, how can I extract number column too?但是我的代码只给出了字符串列,我该如何提取数字列呢?
You can use str.contains
and perform boolean indexing on the dataframe with the result:您可以使用str.contains
并使用结果对数据帧执行布尔索引:
df[df.string.str.lower().str.contains(r'hello')]
string Number
0 hello there 11
1 Hello hi 22
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.