Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除结果位置标记中的方括号_Python_Nltk_Pos Tagger - Fatal编程技术网

Python 如何删除结果位置标记中的方括号

Python 如何删除结果位置标记中的方括号,python,nltk,pos-tagger,Python,Nltk,Pos Tagger,我想从dataframe中提取名词。我做的如下 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

我想从dataframe中提取名词。我做的如下

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['noun']

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

怎么了?

括号表示数据框的每个单元格中都有列表。如果确定每个列表中最多只有一个元素,则可以在名词列上使用
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

括号表示数据框的每个单元格中都有列表。如果确定每个列表中最多只有一个元素,则可以在名词列上使用
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

如果有多个元素呢?如果有多个元素呢?