Pandas count()用法是什么,和values_count()啥不同
Admin 2022-08-30 群英技术资讯 549 次浏览
结果多了该列:
对比:
对比:
1. Series.value_counts(self, normalize=False, sort=True, ascending=False, bins=None, dropna=True)
返回一个包含所有值及其数量的 Series。 且为降序输出,即数量最多的第一行输出。
参数含义如下:
Parameters: | normalize : boolean, default False If True then the object returned will contain the relative frequencies of the unique values. sort : boolean, default True Sort by frequencies. ascending : boolean, default False Sort in ascending order. bins : integer, optional Rather than count values, group them into half-open bins, a convenience for pd.cut, only works with numeric data. dropna : boolean, default True Don’t include counts of NaN. |
---|---|
Returns: | Series |
举例如下:
import pandas as pd index = pd.Index([3, 1, 2, 3, 4, np.nan]) index.value_counts() """ 输出为: 3.0 2 4.0 1 2.0 1 1.0 1 dtype: int64 """
如果 normalize 为 True的话,统计的结果会相加 = 1:
import pandas as pd s = pd.Series([3, 1, 2, 3, 4, np.nan]) s.value_counts(normalize=True) """ 输出为: 3.0 0.4 4.0 0.2 2.0 0.2 1.0 0.2 dtype: float64 """
2. Series.count(self, level=None)
返回非空值的数量。若是在 CSV 文件中可用来统计行数,如:
import pandas as pd file = pd.read_csv('test.csv') print(file['A'].count()) # 此时输出的即是 A 列的行数
参数含义如下:
Parameters: | level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a smaller Series. |
---|---|
Returns: | int or Series (if level specified) Number of non-null values in the Series. |
举例如下:
import pands as pd s = pd.Series([0.0, 1.0, np.nan]) s.count() # 此时输出为 2
这就是两者的区别和各自的用途。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
不管你是做数据分析,还是网络爬虫,Web 开发、亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文
在实际项目开发中,我们通常会根据自己的需求去下载各种相应的框架库,如Scrapy、Beautiful Soup等,但是可能每个项目使用的框架库并不一样,或使用框架的版本不一样,今天给大家分享下Python 虚拟环境的价值和常用命令,感兴趣的朋友一起看看吧
Python注释的用处、类型和语法是什么?下文的讲解详细,步骤过程清晰,对大家进一步学习和理解相关知识有一定的帮助。有这方面学习需要的朋友就继续往下看吧!
django聚合函数的用法是什么?很多新手对于django聚合函数不是很了解,因此这篇文章就主要介绍django聚合函数的用法,有这方面学习需求的朋友可以参考。
这篇文章主要介绍了Python实现Harbor私有镜像仓库垃圾自动化清理详情,文章围绕主题分享相关详细代码,需要的小伙伴可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008