0

I want to call counter values inside pandas.

Effort so far:

from __future__ import unicode_literals
import spacy,en_core_web_sm
from collections import Counter
import pandas as pd
nlp = en_core_web_sm.load()
c = Counter(([token.pos_ for token in nlp('The cat sat on the mat.')]))
sbase = sum(c.values())
for el, cnt in c.items():
    el, '{0:2.2f}%'.format((100.0* cnt)/sbase)
df = pd.DataFrame.from_dict(c, orient='index').reset_index()
print df

Current Output:

   index  0
0   NOUN  2
1   VERB  1
2    DET  2
3    ADP  1
4  PUNCT  1

Expected Output:

The below inside dataframe:

(u'NOUN', u'28.57%')
(u'VERB', u'14.29%')
(u'DET', u'28.57%')
(u'ADP', u'14.29%')
(u'PUNCT', u'14.29%')

I want to call el and cnt inside the data frame how?

It was a folow up question wherein i wanted to get percentage of POS distribution listed.

Percentage Count Verb, Noun using Spacy?

I was of understanding I need to put group el and cnt in place of c below:

df = pd.DataFrame.from_dict(c, orient='index').reset_index()

Programmer_nltk
  • 863
  • 16
  • 38

1 Answers1

2

I can only fix your out put since I do not have the original data

(df['0']/df['0'].sum()).map("{0:.2%}".format)
Out[827]: 
0    28.57%
1    14.29%
2    28.57%
3    14.29%
4    14.29%
Name: 0, dtype: object
BENY
  • 317,841
  • 20
  • 164
  • 234