Pandas count()用法是什么,和values_count()啥不同
Admin 2022-08-30 群英技术资讯 887 次浏览
这篇文章主要介绍“Pandas count()用法是什么,和values_count()啥不同”,有一些人在Pandas count()用法是什么,和values_count()啥不同的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。
结果多了该列:

对比:

对比:

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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
前言上篇文章,讲了经典卷积神经网络-resnet,这篇文章通过resnet网络,做一些具体的事情。一、技术介绍总的来说,第一步首先要加载数据集,对数据进行一些处理,第二步,调整学习率一些
如何理解模块?模块可以看成一个完整的功能非常强的大代码体 使用了模块就相当于你也有这个功能了;使用模块编程就相当于站在巨人的肩膀上(智能语音 人工智能机器学习......)
在做网站项目时,经常会使用脚本生成sitemap, 便于爬虫爬取,有利于SEO。 那么如何使用Python来生成sitemap呢?下面我们来研究一番。安
这篇文章主要介绍了Python flask框架,主要以对flask 微web框架优势特点、demo web应用程序程序步骤以及对flask模块中路由和模版两个功能进行基本了解。具有一定的参考价值,需要的朋友可以参考一下
round函数是python的内置函数,用于数字的四舍五入。round 负数是四舍五入是围绕着0来计算的。如果需要保留其他位数的小数,更改保留位数就可以了。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008