【问题标题】:Faster way to use regex to extract hashtags from tweets使用正则表达式从推文中提取主题标签的更快方法
【发布时间】:2019-06-26 18:04:27
【问题描述】:

我有一个 pandas 数据框,其中包含 100 万条推文的详细信息,包括推文本身和各种其他属性。我正在尝试从推文中提取主题标签列表。重要的是该列表仍与每条推文相关联,而不是所有推文中的主题标签列表。

我拥有的推文数量意味着它需要数小时/数天才能运行。我已经尝试过在我的 pandas 数据帧上使用 iterrows 的替代方法吗?

def extracthash(x):
    for index, row in tweets_scored.iterrows():
    tweets_scored.loc[:,"Hashtags"]= tweets_scored.text.str.find(r'#.*?(?=\s|$)')
    return tweets_scored

tweets_scored.apply(extracthash, axis=1)

这就是我的目标,如果我在我的数据框中只获取少量行的子集,代码确实可以工作。

text                                    hashtag list

I like #cheese and #flour        [#cheese, #flour] 

He eats #bread                            [#bread]

真诚感谢任何帮助!谢谢

【问题讨论】:

  • 您想将其应用于一列?正如你的例子所暗示的那样。

标签: python regex pandas twitter


【解决方案1】:

我将这个小循环用于类似情况(推文上的 NLP)来提取推文的主题标签和 at 引用。它又快又简单:

import re
tHash = []
tAt = []
for item in tweets:
    if re.search('^@.*', item):
       tAt.append(item)

    if re.search('^#.*', item):
       tHash.append(item)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2013-05-17
    • 2014-02-07
    相关资源
    最近更新 更多