Pandas统计计数value_counts()的使用
作者:山茶花开时。 时间:2023-11-24 10:28:46
value_counts()方法返回一个序列Series,该序列包含每个值的数量(对于数据框中的任何列,value_counts()方法会返回该列每个项的计数)
value_counts()是Series拥有的方法,一般在DataFrame中使用时,需要指定对哪一列进行使用
语法
value_counts(values,
sort=True,
ascending=False,
normalize=False,
bins=None,
dropna=True)
参数说明
sort: 是否要进行排序(默认进行排序,取值为True)
ascending: 默认降序排序(取值为False),升序排序取值为True
normalize: 是否要对计算结果进行标准化,并且显示标准化后的结果,默认是False
bins: 可以自定义分组区间,默认是否
dropna: 是否包括对NaN进行计数,默认不包括
import pandas as pd
import numpy as np
df = pd.DataFrame({'City': ['北京', '广州', '深圳', '上海', '大连', '成都', '深圳', '厦门', '北京', '北京', '上海', '珠海'],
'Revenue': [10000, 10000, 5000, 5000, 40000, 50000, 8000, 5000, 5000, 5000, 10000, 12000],
'Age': [50, 43, 34, 40, 25, 25, 45, 32, 25, 25, 34, np.nan]})
# 1.查看'City'这一列的计数结果(对给定列里面的每个值进行计数并进行降序排序,缺失值nan也会被排除)
# value_counts()并不是未带任何参数,而是所有参数都是默认的
res1 = df['City'].value_counts()
# 2.查看'Revenue'这一列的计数结果(采用升序的方式)
res2 = df['Revenue'].value_counts(ascending=True)
# 3.查看'Age'这一列的计数占比(使用标准化normalize=True)
res3 = df['Age'].value_counts(ascending=True,normalize=True)
# 4.查看'Age'这一列的计数结果(展示NaN值的计数)
res4 = df['Age'].value_counts(dropna=False)
# 5.查看'Age'这一列的计数结果(不展示NaN值的计数)
# res5 = df['Age'].value_counts()
res5 = df['Age'].value_counts(dropna=True)
df
res1
res2
res3
res4
res5
来源:https://blog.csdn.net/Hudas/article/details/125389653
标签:Pandas,统计计数,value,counts()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue项目页面嵌入代码块vue-prism-editor的实现
2024-04-27 16:14:03
![](https://img.aspxhome.com/file/2023/4/132934_0s.png)
JDBC获取数据库连接的5种方式实例
2024-01-19 05:14:05
python第三方库visdom的使用入门教程
2021-12-08 22:32:51
![](https://img.aspxhome.com/file/2023/9/82719_0s.png)
JDBC如何获取数据库连接
2024-01-23 05:53:50
快速解决jupyter notebook启动需要密码的问题
2023-09-27 23:56:49
![](https://img.aspxhome.com/file/2023/9/97039_0s.jpg)
asp form 表单验证函数
2011-04-04 11:10:00
go语言中函数与方法介绍
2024-04-23 09:34:13
完美解决MySQL通过localhost无法连接数据库的问题
2024-01-26 17:41:10
python 遍历可迭代对象的实现方法
2021-03-02 07:13:07
JavaScript实现简单MD5加密的脚本分享
2023-07-02 03:47:49
Python 将json序列化后的字符串转换成字典(推荐)
2021-11-17 12:36:51
Python Logging 日志记录入门学习
2022-05-17 14:48:39
python判断windows系统是32位还是64位的方法
2023-08-08 15:17:04
Javascript 逗号“,”引发的血案
2008-08-20 18:37:00
利用Python破解斗地主残局详解
2021-06-04 06:16:49
![](https://img.aspxhome.com/file/2023/2/92342_0s.jpg)
python判断列表的连续数字范围并分块的方法
2021-01-18 12:04:19
浅析Python数据处理
2023-09-08 18:18:34
Python模拟登录验证码(代码简单)
2022-09-04 04:41:37
Python入门篇之字符串
2022-01-09 00:31:17
Python实现智能贪吃蛇游戏的示例代码
2023-01-28 13:39:36
![](https://img.aspxhome.com/file/2023/1/100861_0s.jpg)