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>

<h3>Setup reference</h3>

<pre><code>s = pd.Series([
        [('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')]
    ], name='comment')

s

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...
Name: comment, dtype: object
</code></pre>

<hr>

<h3>Solution</h3>

<pre><code>s1 = s.apply(pd.Series).stack().apply(pd.Series)
s2 = s1.loc[s1[1].isin(['VERB', 'NOUN']), 0]
s3 = s2.groupby(level=0).apply(zip).reindex_like(s)
s3.loc[s3.isnull()] = [[np.nan]]
s3

0                  [(have,), (pahae,)]
1    [(radio,), (television,), (lid,)]
2                                [nan]
3                            [(work,)]
4              [(debt,), (increased,)]
Name: 0, dtype: object
</code></pre>

<hr>

<p><strong><em>For Python 3</em></strong><br>
per @jezrael</p>

<pre><code>s1 = s.apply(pd.Series).stack().apply(pd.Series)
s2 = s1.loc[s1[1].isin(['VERB', 'NOUN']), 0]
s3 = s2.groupby(level=0).apply(lambda x: list(zip(x))).reindex_like(s)
s3.loc[s3.isnull()] = [[np.nan]]
s3
</code></pre>

python list pandas tuples nltk