我有一个熊猫数据框,其中包含100万条推文的详细信息,包括推文本身和其他各种属性。我正在尝试从推文中提取主题标签列表。重要的是该列表仍与每个推文相关联,而不是成为所有推文中的主题标签的列表。
我拥有的推文数量意味着运行将需要数小时/天。正如我已经尝试过的那样,除了在熊猫数据框上使用迭代之外,还有其他选择吗?
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]
任何帮助都由衷地感谢!谢谢
答案 0 :(得分: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)