【问题标题】:extracting specific information from string with varying patterns从具有不同模式的字符串中提取特定信息
【发布时间】:2021-06-26 11:18:02
【问题描述】:
import pandas as pd
df = pd.DataFrame({'Reference':["PO: TK42-8", 
                                "PO GQ5-42", 
                                "PO:HEA-238/239", 
                                "PO: 4501005609  Purchaser: Mariana Toledo Blanco", 
                                "FITN7-26", 
                                "PO#CP4-62",
                                "PO 4501004752  Purchaser Yang Gao / Split from S94964",
                                "GUANGDONG YOULONG ELECTRICAL APPLIANCES CO.,LTD // PO#GQY6-17"]
                   })

从上面的 df 中,我一直在尝试提取两条信息(如果有的话),但成功率最低。从而创建 2 个新列,如下面所需的 df 所示。

df2 = pd.DataFrame({'Reference':["PO: TK42-8", 
                                "PO GQ5-42", 
                                "PO:HEA-238/239", 
                                "PO: 4501005609  Purchaser: Mariana Toledo Blanco", 
                                "FITN7-26", 
                                "PO#CP4-62",
                                "PO 4501004752  Purchaser Yang Gao / Split from S94964",
                                "GUANGDONG YOULONG ELECTRICAL APPLIANCES CO.,LTD // PO#GQY6-17"],
                    
                    "PO":["TK42-8", "GQ5-42", "HEA-238/239", "4501005609", "FITN7-26","CP4-62", "4501004752", "GQY6-17" ],
                    "Purchaser":["", "", "", "Mariana Toledo Blanco", "","", "Yang Gao", "" ],
                   })

到目前为止,我已经看到了一些成功:

df['PO'] = df['Reference'].str.extract(r"PO:.*?([ \w.\S-]+)")
df['Purchaser'] = df['Reference'].str.extract(r"Purchaser.*?([ \w.*]+)")

但是,我错过了如何正确编写每个函数括号内每种情况的所有微妙可能性的脚本。

【问题讨论】:

  • 看看你的数据框,我认为你可以使用r'PO\W+(\S+)' 来提取PO 和提取Purchaser 使用r'Purchaser\W+((?:\w+\s?)+)(?:\s|$)'
  • @ShubhamSharma 不完全是。我更接近以下内容,但仍然缺少一些值。 df['PO'] = df['Reference'].str.extract(r"PO:\S|\s.*?([\w.\S-]+)") df['Purchaser'] = df['Reference'].str.extract(r"Purchaser.*?([ \w.*]+)")
  • "FITN7-26" 的条目上出现了一个问题。它前面没有PO

标签: python regex pandas


【解决方案1】:

提取采购订单
>>> df['Reference'].str.extract(r"(?:^(?=[A-Z\d/-]+$)|\bPO\W*)([A-Z\d/-]+)")
             0
0       TK42-8
1       GQ5-42
2  HEA-238/239
3   4501005609
4     FITN7-26
5       CP4-62
6   4501004752
7      GQY6-17

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    ^                        the beginning of the string
--------------------------------------------------------------------------------
    (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
      [A-Z\d/-]+               any character of: 'A' to 'Z', digits
                               (0-9), '/', '-' (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
      $                        before an optional \n, and the end of
                               the string
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    PO                       'PO'
--------------------------------------------------------------------------------
    \W*                      non-word characters (all but a-z, A-Z, 0-
                             9, _) (0 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [A-Z\d/-]+               any character of: 'A' to 'Z', digits (0-
                             9), '/', '-' (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1

提取购买者
>>> df['Reference'].str.extract(r"\bPurchaser\W+(\w(?:[\s\w]*\w)?)").fillna("")
                       0
0                       
1                       
2                       
3  Mariana Toledo Blanco
4                       
5                       
6               Yang Gao
7                       

解释

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  Purchaser                'Purchaser'
--------------------------------------------------------------------------------
  \W+                      non-word characters (all but a-z, A-Z, 0-
                           9, _) (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \w                       word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      [\s\w]*                  any character of: whitespace (\n, \r,
                               \t, \f, and " "), word characters (a-
                               z, A-Z, 0-9, _) (0 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
      \w                       word characters (a-z, A-Z, 0-9, _)
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \1

【讨论】:

  • 绝对精彩!非常感谢您的解释,这对于将来学习这一点真的有很长的路要走。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
相关资源
最近更新 更多