首页 > 解决方案 > 在python 3中查找表中名字第一个字符的频率分布

问题描述

我有一张像

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

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

c 2
p 2
s 1

标签: pythonpython-3.xpython-2.7pandasjupyter-notebook

解决方案


通过索引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_axis_reset_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

推荐阅读