2016-09-06 117 views
0

我想從數據框中提取名詞。我做如下如何刪除結果中的方括號pos_tag

import pandas as pd 
import nltk 
from nltk.tag import pos_tag 
df = pd.DataFrame({'pos': ['noun', 'Alice', 'good', 'well', 'city']}) 
noun=[] 
for index, row in df.iterrows(): 
    noun.append([word for word,pos in pos_tag(row) if pos == 'NN']) 
df['noun'] = noun 

,我也得到DF [ '名詞']

0  [noun] 
1 [Alice] 
2   [] 
3   [] 
4  [city] 

我用正則表達式

df['noun'].replace('[^a-zA-Z0-9]', '', regex = True) 

,並再次

0  [noun] 
1 [Alice] 
2   [] 
3   [] 
4  [city] 
Name: noun, dtype: object 

有什麼不對?

回答

2

括號表示您在數據框的每個單元格中都有列表。如果你相信有隻有一個元素是最多每個列表中,您可以在名詞列中使用str,並提取第一個元素:

df['noun'] = df.noun.str[0] 

df 
# pos noun 
#0 noun noun 
#1 Alice Alice 
#2 good NaN 
#3 well NaN 
#4 city city 
+0

如果什麼有多個元素? – Enthusiast