简体   繁体   English

Python 正则表达式 DataFrame 匹配

[英]Python Regex DataFrame match

I have a DataFrame and I would like to perform a sorting if the match between my regex and the name of one of the lines of this DataFrame matches.我有一个 DataFrame,如果我的正则表达式与此 DataFrame 的其中一行的名称匹配,我想执行排序。 Is there an option in the "re" library to help me? “重新”库中有一个选项可以帮助我吗? I try with this piece of code but without success Thank you in advance for your answers我尝试使用这段代码但没有成功提前谢谢你的回答

regex = r"D.*cube"

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

I think you probably want我想你可能想要

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...
          .............
    

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

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