【问题标题】:A Regular expression to find second occurence in DataFrame在 DataFrame 中查找第二次出现的正则表达式
【发布时间】:2021-04-15 16:29:18
【问题描述】:

我在数据框中有一个字符串列,我想从中提取一个速率,这是最后一次出现的反斜杠。

 After so many requests, this is Bretagne. She was the last surviving 9/11 search dog, and our second ever 14/10. RIP* 

我想得到14/10

【问题讨论】:

  • df[column].str.extract(\d+\/\d+.*?(\d+\/\d+))?
  • @politicalscientist 没用,但还是谢谢。

标签: python regex pandas dataframe


【解决方案1】:

尝试使用此代码获取这句话中所有日期的列表

import re
re.findall(r"\d+/\d+","After so many requests, this is Bretagne. She was the last surviving 9/11 search dog, and our second ever 14/10. RIP* ")

要获得第二个,只需选择此列表中的第二个项目

【讨论】:

    【解决方案2】:

    这个正则表达式有效:

    >>> s = "After so many requests, this is Bretagne. She was the last surviving 9/11 search dog, and our second ever 14/10. RIP*"
    >>> re.findall("\d+/(?=[^/]*$)\d+", s)
    ['14/10']
    

    【讨论】:

    • @DODE 没问题,因为我的回答给了你更简洁的正则表达式的直接结果,你应该考虑接受这个答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多