简体   繁体   English

搜索一列中的字符串并提取匹配行 Python 的所有列

[英]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.

相关问题 如何在列的所有条目中提取包含部分匹配字符串的行 - how to extract rows that contains partially matched strings in all entries of a column Python:如果在一列中匹配的词典键替换,则另一列具有值 - Python: If Dictionary key matched in one column replace, another column with value Python3:在csv文件中搜索字符串并返回行和列 - Python3: search csv file for string and return row and column 从Python中的字符串中提取列 - Extract a column from a string in Python 比较行列并打印匹配的列 - Compare row and column and print the matched column 搜索列,如果值不是所有数字,则将值剪切并粘贴到同一行的另一列 python pandas - Search column, if value is not all digits, then cut&paste value to another column same row python pandas 如何使用python在csv中提取列和行 - How to extract column and row in csv using python python 在列中搜索字符串,并从同一行返回另一个列值 - python Search a string in a column, and return another column value from that same row 如何在python中另一列的字符串值中的数据框中的一列中搜索字符串? - How can i search for a string from one column in a dataframe in a string value in another column in python? Python Pandas:检查一列中的字符串是否包含在同一行中另一列的字符串中 - Python Pandas: Check if string in one column is contained in string of another column in the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM