广告
返回顶部
首页 > 资讯 > 后端开发 > Python >关于Pandas count()与values_count()的用法及区别
  • 371
分享到

关于Pandas count()与values_count()的用法及区别

2024-04-02 19:04:59 371人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录pandas count()与values_count()用法count()values_count()在指定的统计的列名上Pandas:count()与value_counts

Pandas count()与values_count()用法

count()

values_count()在指定的统计的列名上

结果多了该列:

对比:

对比:

Pandas:count()与value_counts()对比

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

这就是两者的区别和各自的用途。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 关于Pandas count()与values_count()的用法及区别

本文链接: https://www.lsjlt.com/news/117947.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作