How to get only word for selected tag in NLTK Part of Speech (POS) tagging?
<p>Sorry I am new to Pandas and NLTK. I'm trying to build set of customize returned POS. My data contents:</p>

<pre><code>        comment
0       [(have, VERB), (you, PRON), (pahae, VERB)]
1       [(radio, NOUN), (television, NOUN), (lid, NOUN)]
2       [(yes, ADV), (you're, ADJ)]
3       [(ooi, ADJ), (work, NOUN), (barisan, ADJ)]
4       [(national, ADJ), (debt, NOUN), (increased, VERB)]
</code></pre>

<p>Any idea how can I get only word that match selected tag (<code>VERB</code> or <code>NOUN</code>), like below? And return <code>NaN</code> if none matching.</p>

<pre><code>        comment
0       [(have), (pahae)]
1       [(radio), (television), (lid)]
2       [NaN]
3       [(work)]
4       [(debt), (increased)]
</code></pre>

<p>You can use <code>list comprehension</code> and then replace empty <code>list</code> to <code>[NaN]</code>:</p>

<pre><code>df = pd.DataFrame({'comment': [
        [('have', 'VERB'), ('you', 'PRON'), ('pahae', 'VERB')],
        [('radio', 'NOUN'), ('television', 'NOUN'), ('lid', 'NOUN')],
        [('yes', 'ADV'), ("you're", 'ADJ')],
        [('ooi', 'ADJ'), ('work', 'NOUN'), ('barisan', 'ADJ')],
        [('national', 'ADJ'), ('debt', 'NOUN'), ('increased', 'VERB')]
    ]})

print (df)    
                                             comment
0         [(have, VERB), (you, PRON), (pahae, VERB)]
1   [(radio, NOUN), (television, NOUN), (lid, NOUN)]
2                        [(yes, ADV), (you're, ADJ)]
3         [(ooi, ADJ), (work, NOUN), (barisan, ADJ)]
4  [(national, ADJ), (debt, NOUN), (increased, VE...
</code></pre>



<pre><code>df.comment = df.comment.apply(lambda x: [(t[0],) for t in x if t[1]=='VERB' or t[1]=='NOUN'])
df.ix[df.comment.apply(len) == 0, 'comment'] = [[np.nan]]
print (df)
                             comment
0                [(have,), (pahae,)]
1  [(radio,), (television,), (lid,)]
2                              [nan]
3                          [(work,)]
4            [(debt,), (increased,)]
</code></pre>

python list pandas tuples nltk