【问题标题】:Counter calling in pandas?大熊猫的反电话?
【发布时间】:2019-01-31 10:54:04
【问题描述】:

我想在 pandas 中调用计数器值。

目前的努力:

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

电流输出:

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

预期输出:

下面的内部数据框:

(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%')

我想在数据框里面怎么调用el和cnt?

这是一个后续问题,我想列出列出的 POS 分布百分比。

Percentage Count Verb, Noun using Spacy?

我知道我需要将组 el 和 cnt 放在下面的 c 位置:

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

【问题讨论】:

    标签: pandas


    【解决方案1】:

    由于我没有原始数据,我只能修复您的输出

    (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
    

    【讨论】:

    • (df.iloc[:,1]/df.iloc[:,1].sum()).map("{0:.2%}".format)@Programmer_nltk
    • @Programmer_nltk 再次检查,我添加了.
    猜你喜欢
    • 2020-05-04
    • 2018-09-28
    • 2016-11-25
    • 2018-06-29
    • 2014-08-18
    • 2018-09-14
    • 1970-01-01
    • 2014-10-04
    • 2020-09-13
    相关资源
    最近更新 更多