【问题标题】:Pandas apply does nothing with regex functionPandas apply 对正则表达式函数没有任何作用
【发布时间】:2021-11-28 04:56:43
【问题描述】:

感谢您阅读并(希望)提供帮助! 我被熊猫申请难住了。我在正则表达式函数上使用它,该函数在普通字符串上工作得很好,但是当我在数据帧上使用它时,它只输出相同的单元格值。函数如下:

def match_pattern(df_cell): 
   if type(df_cell) == str:
        result = re.search(r'(?:[0-9]{1,4}\s)(.*)(?=\nName)', df_cell)
        if result:
            print('result.group(1)',result.group(1))
            return result.group(1)
        else:
            print('no result')
            return df_cell
    else:
        return df_cell

现在这在字符串上工作得很好。例如:

string = '3971 Small Arms Survey\nName'
string2 = 'nothing here'
match_pattern(string) # outputs 'Small Arms Survey' which is what i want
match_pattern(string2) # outputs 'nothing here'

但是当我在带有 apply 的数据框上使用它时似乎不起作用

frame = pd.DataFrame(['3971 Small Arms Survey\nName'])
frame2 = frame.apply(lambda x: match_pattern(str(x)))
frame2 # outputs '3971 Small Arms Survey\nName'

我会尝试其他的东西,比如 iterrows 或 itertuples 等,但最终这个正则表达式函数应该用于大型数据帧的每个单元格,任何比 apply 慢的东西几乎都不可行。

match_pattern() 函数中的打印语句仅用于调试。如果您想知道,print('result.group(1)',result.group(1)) 字符串会同时触发:“字符串”上的应用程序和数据帧上的应用程序。但是打印输出不一样。在这两种情况下,打印输出都是函数返回的内容,在数据帧的情况下,它只是数据帧中开始的字符串,而对于字符串,打印输出是我想要过滤的字符串(即 group( 1)在函数内部的正则表达式中)。

非常感谢 Wiktor Stribiżew,他的评论回答了我的问题!原来这是一个简单而愚蠢的错误。在数据框的列上使用 apply 将起作用:

frame = frame[0].apply(match_pattern) # outputs 'Small Arms Survey' for the cell, which is what i want

【问题讨论】:

    标签: python regex pandas apply


    【解决方案1】:

    您可以在0th 列上运行apply

    import re
    import pandas as pd
    
    def match_pattern(df_cell): 
       if isinstance(df_cell, str):
            result = re.search(r'[0-9]{1,4}\s(.*)\nName', df_cell)
            if result:
                print('result.group(1)',result.group(1))
                return result.group(1)
            else:
                print('no result')
                return df_cell
       else:
            return df_cell
    
    frame = pd.DataFrame(['3971 Small Arms Survey\nName'])
    frame[0] = frame[0].apply(match_pattern)
    # => frame
    #                    0
    # 0  Small Arms Survey
    

    请注意,我将正则表达式简化为 [0-9]{1,4}\s(.*)\nName,因为您只需要捕获到组 1 中的文本。

    另外,if isinstance(df_cell, str): 恕我直言,检查df_cell 的类型看起来更整洁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      相关资源
      最近更新 更多