【发布时间】:2018-03-19 21:56:14
【问题描述】:
我有一个类似这样的 csv 文件
text
RT @CritCareMed: New Article: Male-Predominant Plasma Transfusion Strategy for Preventing Transfusion-Related Acute Lung Injury... htp://…
#CRISPR Inversion of CTCF Sites Alters Genome Topology & Enhancer/Promoter Function in @CellCellPress htp://.co/HrjDwbm7NN
RT @gvwilson: Where's the theory for software engineering? Behind a paywall, that's where. htp://.co/1t3TymiF3M #semat #fail
RT @sciencemagazine: What’s killing off the sea stars? htp://.co/J19FnigwM9 #ecology
RT @MHendr1cks: Eve Marder describes a horror that is familiar to worm connectome gazers. htp://.co/AEqc7NOWoR via @nucAmbiguous htp://…
我想从推文中提取所有提及(以“@”开头)。到目前为止,我已经这样做了
import pandas as pd
import re
mydata = pd.read_csv("C:/Users/file.csv")
X = mydata.ix[:,:]
X=X.iloc[:,:1] #I have multiple columns so I'm selecting the first column only that is 'text'
for i in range(X.shape[0]):
result = re.findall("(^|[^@\w])@(\w{1,25})", str(X.iloc[:i,:]))
print(result);
这里有两个问题:
首先:在str(X.iloc[:1,:]),它给了我['CritCareMed'],这是不行的,因为它应该给我['CellCellPress'],在str(X.iloc[:2,:]),它又给了我['CritCareMed'],这当然又不好了。我得到的最终结果是
[(' ', 'CritCareMed'), (' ', 'gvwilson'), (' ', 'sciencemagazine')]
它不包括第二行中的提及和最后一行中的两个提及。 我想要的应该是这样的:
我怎样才能达到这些结果?这只是一个示例数据,我的原始数据有很多推文,所以这种方法可以吗?
【问题讨论】:
标签: python regex pandas twitter mention