【问题标题】:Find the frequency distribution of the first character of the name in the table in python 3在python 3中查找表中名字第一个字符的频率分布
【发布时间】:2019-04-17 11:55:19
【问题描述】:

我有一张这样的桌子

key Name
 1   snake
 2   panda
 3   parrot
 4   catipie
 5   cattie

现在我想找到每行第一个字符的出现次数并按降序排序,如果有平局,它应该按词汇顺序排序,所以我的输出如下所示:

c 2
p 2
s 1

【问题讨论】:

    标签: python python-3.x python-2.7 pandas jupyter-notebook


    【解决方案1】:

    通过索引str[0] 选择第一个值并按value_counts 计数:

    s = df['Name'].str[0].value_counts()
    print (s)
    p    2
    c    2
    s    1
    Name: Name, dtype: int64
    

    对于DataFrame 添加rename_axisreset_index

    df = df['Name'].str[0].value_counts().rename_axis('first').reset_index(name='count')
    print (df)
      first  count
    0     p      2
    1     c      2
    2     s      1
    

    如果需要按字母排序相同的计数添加sort_values:

    df = df.sort_values(['first','count'], ascending=[True, False])
    print (df)
      first  count
    1     c      2
    0     p      2
    2     s      1
    

    对于系列:

    s = df.set_index('first')['count']
    print (s)
    first
    c    2
    p    2
    s    1
    Name: count, dtype: int64
    

    最后使用to_string

    print (s.to_string(header=None))
    c    2
    p    2
    s    1
    

    【讨论】:

    • 谢谢@jezrael,这给了我一个想法。我想将其输出为一个没有任何列名的系列,如果有平局,则应按降序排序并按词法升序
    • p 在 panda , p 在 parrot , p 在 catipie, 使它成为 3
    • 谢谢 :) 但它在我的测试集上失败了。你能检查一下吗
    • @Mighty - 什么测试?
    猜你喜欢
    • 2017-03-26
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多