【问题标题】:Python Regex DataFrame matchPython 正则表达式 DataFrame 匹配
【发布时间】:2021-04-01 13:50:32
【问题描述】:

我有一个 DataFrame,如果我的正则表达式与此 DataFrame 的其中一行的名称匹配,我想执行排序。 “重新”库中有一个选项可以帮助我吗? 我尝试使用这段代码但没有成功 提前感谢您的回答

regex = r"D.*cube"

for row in range(len(df)):
    for col in range(len(df.columns)):
        
        if df.iloc[row, col] == regex:
          #treatment...
          .............
    
    

【问题讨论】:

    标签: python regex dataframe iteration


    【解决方案1】:

    我想你可能想要

    pattern = r"D.*cube"
    
    for row in range(len(df)):
        for col in range(len(df.columns)):
            
            if re.match(pattern, df.iloc[row, col]):
              #treatment...
              .............
        
    

    【讨论】:

    • 感谢 Nicholas 的回复。不幸的是,它仍然不起作用。我最终得到了这个错误...... ----> 6 if re.match(pattern, dfc.iloc[row, col]): 189 """尝试在字符串的开头应用模式,返回 190匹配对象,如果没有找到匹配项,则返回 None。""" --> 191 return _compile(pattern, flags).m​​atch(string) 192 193 def fullmatch(pattern, string, flags=0): TypeError: expected string or字节类对象
    • 问题解决了,感谢您的 Nicholas 解决方案。我还必须通过指示来强制比较:str(dfc.iloc [row, col])) 再次,非常感谢:D
    猜你喜欢
    • 2016-07-01
    • 2013-09-22
    • 2022-12-11
    • 2015-09-14
    • 2021-12-31
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多