How to extract nouns from dataframe
<p>I want to extract nouns from dataframe. Only nouns.
I do as below</p>

<pre><code>import pandas as pd
import nltk
from nltk.tag import pos_tag
from nltk import word_tokenize
df = pd.DataFrame({'noun': ['good day', 'good night']})
</code></pre>

<p>I want to get</p>

<pre><code>    noun
0   day
1   night
</code></pre>

<p>My code    </p>

<pre><code>df['noun'] = df.apply(lambda row: nltk.word_tokenize(row['noun']), axis=1) 
noun=[]
for  index, row in df.iterrows():
    noun.append([word for word,pos in pos_tag(row) if pos == 'NN'])
df['noun'] = noun 



 ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-194-688cfbb21ec5&gt; in &lt;module&gt;()
      1 noun=[]
      2 for  index, row in df.iterrows():
----&gt; 3     noun.append([word for word,pos in pos_tag(row) if pos == 'NN'])
      4 df['noun'] = noun

C:\Users\Edward\Anaconda3\lib\site-packages\nltk\tag\__init__.py in pos_tag(tokens, tagset)
    109     """
    110     tagger = PerceptronTagger()
--&gt; 111     return _pos_tag(tokens, tagset, tagger)
    112 
    113 

C:\Users\Edward\Anaconda3\lib\site-packages\nltk\tag\__init__.py in _pos_tag(tokens, tagset, tagger)
     80 
     81 def _pos_tag(tokens, tagset, tagger):
---&gt; 82     tagged_tokens = tagger.tag(tokens)
     83     if tagset:
     84         tagged_tokens = [(token, map_tag('en-ptb', tagset, tag)) for (token, tag) in tagged_tokens]

C:\Users\Edward\Anaconda3\lib\site-packages\nltk\tag\perceptron.py in tag(self, tokens)
    150         output = []
    151 
--&gt; 152         context = self.START + [self.normalize(w) for w in tokens] + self.END
    153         for i, word in enumerate(tokens):
    154             tag = self.tagdict.get(word)

C:\Users\Edward\Anaconda3\lib\site-packages\nltk\tag\perceptron.py in &lt;listcomp&gt;(.0)
    150         output = []
    151 
--&gt; 152         context = self.START + [self.normalize(w) for w in tokens] + self.END
    153         for i, word in enumerate(tokens):
    154             tag = self.tagdict.get(word)

C:\Users\Edward\Anaconda3\lib\site-packages\nltk\tag\perceptron.py in normalize(self, word)
    222         if '-' in word and word[0] != '-':
    223             return '!HYPHEN'
--&gt; 224         elif word.isdigit() and len(word) == 4:
    225             return '!YEAR'
    226         elif word[0].isdigit():

AttributeError: 'list' object has no attribute 'isdigit'
</code></pre>

<p>Please, help, how to improve it?
* Sorry, i have ro write some text so that i can insert all traceback
I guess thr problem is that i cann't convert list to needed format?</p>

<p>The problem is that in your loop, <code>row</code> is a pandas <code>Series</code> rather than a list. You can access the list of words by writing <code>row[0]</code> instead:</p>

<pre><code>&gt;&gt;&gt; for  index, row in df.iterrows():
&gt;&gt;&gt;     noun.append([word for word,pos in pos_tag(row[0]) if pos == 'NN'])
&gt;&gt;&gt; print(noun)
[['day'], ['night']]
</code></pre>

<p>Here you're getting a list of lists, with each list containing the nouns from one sentence. If you really want a flat list (as in the sample result in your question), write <code>noun.extend(...)</code> instead of <code>noun.append</code>. </p>

python nltk pos-tagger