无法将提取的POS taged Noun短语附加到pandas数据框

时间:2017-12-10 10:48:39

标签: python pandas nlp

我试图只提取名词和名词短语来解决数据(csv文件中的一列)。

我能够从数据中删除停用词,标点符号和数字。也能够POS标记数据,但不能提取名词短语并附加回数据框。让我知道出了什么问题

    stopwords=nltk.corpus.stopwords.words('english')
    user_defined_stop_words=['hong','kong','hk','kowloon','hongkong']                    
    new_stop_words=stopwords+user_defined_stop_words

    data['Clean_addr'] = data['Adj_Addr'].apply(lambda x: ' '.join([item.lower() for item in x.split()]))
    data['Clean_addr']=data['Clean_addr'].apply(lambda x:"".join([item.lower() for item in x if  not  item.isdigit()]))
    data['Clean_addr']=data['Clean_addr'].apply(lambda x:"".join([item.lower() for item in x if item not in string.punctuation]))
    data['Clean_addr'] = data['Clean_addr'].apply(lambda x: ' '.join([item.lower() for item in x.split() if item not in (new_stop_words)]))

texts = data['Clean_addr'].tolist()
tagged_texts = pos_tag_sents(map(word_tokenize, texts))
data['POS']=tagged_texts
data['POS']=data['POS'].apply(lambda x:' '.join([item[0] for item in x if (item[0][1]=='NNP' or item[0][1]=='NNS')]))    

我正在使用的文件的样本转储

https://www.dropbox.com/s/allhfdxni0kfyn6/Test.csv?dl=0

1 个答案:

答案 0 :(得分:1)

基于链接的数据:

data['POS'].apply(lambda x : ','.join([i[0] for i in x if (i[1]=='NNS' or i[1] =='NNP')]))

0               des
1               des
2           cfa,des
3     registrations
4                  
5            floors
6            queens
7            queens
8            queens
9                  
10       solicitors
11                 
12                 
13                 
14                 
15              des
Name: POS, dtype: object