代码之家  ›  专栏  ›  技术社区  ›  Nick Duddy

regex是将一个字符串系列拆分为多个包含部分字符串的系列的最佳方法吗?

  •  0
  • Nick Duddy  · 技术社区  · 6 年前

    我正试图用熊猫清理一些数据。数据可以下载 here .

    这是价格数据,但有些用户已经在字段中输入了其他数据类型,我想将其移动到自己的系列中。雷格克斯立刻出现在我的头上,但我想知道是否还有其他更容易的选择?

    有问题的数据以这种格式出现:

    99,0006,000 on 13-7-2018

    好的数据以这种格式显示:

    99,500

    我要创建的输出是3个新系列

    第1列:2018年7月13日

    COL2:99000

    COL3:6000

    我需要将日期分开,但也要将99000和6000分开,因为这是两个单独的数字(人们已经输入了价格范围)。当regex中的值每次都发生变化时,是否拆分中间值?

    欢迎就如何最好地执行此操作提出任何建议:)

    2 回复  |  直到 6 年前
        1
  •  0
  •   Nick Duddy    6 年前

    splitprice_pt1 = pricedata.str.split(r'\bon\b', expand=True)[0]
    #split the numbers on the 'on' text and selecting the first part
    split_KE_Date = pricedata.str.split(r'\bon\b', expand=True)[1]
    #split the numbers on the 'on' text and selecting the second part
    split_KE_Price = splitprice_pt1.str.split(r'^([^,]*,[^,]{3})', expand=True)[0]
    #split the numbers on the 3 digits after comma and selecting the first part
    range_KE_Price = splitprice_pt1.str.split(r'^([^,]*,[^,]{3})', expand=True)[1]
    #split the numbers on the 3 digits after comma and selecting the second part
    
        2
  •  0
  •   Mithil Bhoras    6 年前

    str1 = "99,0006,000                 on 13-7-2018"
    num1 = str1[0:6]
    num2 = str2[6:11]
    num1 = 99,000
    num2 = 6,000
    

    date_pt = re.compile("[0-9]{2}-[0-9]{2}-[0-9]{4}")
    date = re.search(str1)