【问题标题】:Remove a URL row by row from a large set of text in python panda dataframe从 python panda 数据框中的大量文本中逐行删除 URL
【发布时间】:2018-01-05 19:59:15
【问题描述】:

我已将数据插入到 pandas 数据框中。像图片建议的那样 正如您所看到的,有些行包含 url 链接我想删除所有 url 链接并用“”替换它们(没有只是擦除它) 你可以看到第 4 行有一个 url 还有其他行有网址。我想通过 status_message 列中的所有行找到任何 url 并删除它们。我一直在看这个How to remove any URL within a string in Python,但不确定如何在数据帧上使用它。所以第 4 行现在应该投票支持劳工登记。

【问题讨论】:

    标签: python regex pandas dataframe spyder


    【解决方案1】:

    您可以将str.replacecase=False 参数一起使用:

    df = pd.DataFrame({'status_message':['a s sd Www.labour.com',
                                        'httP://lab.net dud ff a',
                                         'a ss HTTPS://dd.com ur o']})
    print (df)
                 status_message
    0     a s sd Www.labour.com
    1   httP://lab.net dud ff a
    2  a ss HTTPS://dd.com ur o
    
    df['status_message'] = df['status_message'].str.replace('http\S+|www.\S+', '', case=False)
    print (df)
      status_message
    0        a s sd 
    1       dud ff a
    2     a ss  ur o
    

    【讨论】:

    • 是的,非常相似,只有一个区别 - case=False 不区分大小写。
    • case = False加一
    【解决方案2】:

    您可以使用.replace() 和正则表达式来做到这一点,即

    df = pd.DataFrame({'A':['Nice to meet you www.xy.com amazing','Wow https://www.goal.com','Amazing http://Goooooo.com']})
    df['A'] = df['A'].replace(r'http\S+', '', regex=True).replace(r'www\S+', '', regex=True)
    

    输出:

    一种 0 很高兴认识你 1 哇 2 惊人的

    【讨论】:

      【解决方案3】:

      我认为你可以做一些简单的事情

      for index,row in data.iterrows():
          desc = row['status_message'].lower().split()
          print ' '.join(word for word in desc if not word.startswith(('www.','http')))
      

      只要网址以“www”开头。

      【讨论】:

        【解决方案4】:

        df.status_message = df.status_message.str.replace("www.", "")

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-20
          • 2020-05-28
          • 1970-01-01
          • 2021-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多