python - 在python 3中查找表中名字第一个字符的频率分布
问题描述
我有一张像
key Name
1 snake
2 panda
3 parrot
4 catipie
5 cattie
现在我想找到每行第一个字符的出现次数并按降序排序,如果有平局,它应该按词汇顺序排序,所以我的输出如下所示:
c 2
p 2
s 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_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
推荐阅读
- django - 无法使用带有用户名和密码的过滤器获取用户
- html - React - 取消选中复选框不会使 div 消失
- c# - EF Core InMemory 未抛出:无法保存更改,因为检测到循环依赖
- android - 取消注册地理围栏的延迟
- python - 如何使用整数迭代列表中索引的值
- c# - c#中判断json是数组还是对象的通用方法
- python - 为什么numpy.save没有在linux上保存文件
- bash - 在 bash 中为每个“通配符”参数添加一些文本
- python-3.x - 熊猫数据框逐行迭代,并引用前一行值进行条件匹配
- kubernetes - Kubernetes - 将静态 IP 路由到多个服务(谷歌云平台)