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

作者:Elvirangel 时间:2021-09-25 08:28:20 

Pandas count()与values_count()用法

count()

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

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

结果多了该列:

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

对比:

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

对比:

关于Pandas 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

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

来源:https://blog.csdn.net/Elvirangel/article/details/104556394

标签:Pandas,count,values,count
0
投稿

猜你喜欢

  • python使用正则表达式的search()函数实现指定位置搜索功能

    2023-08-08 09:26:01
  • Django框架视图介绍与使用详解

    2021-03-29 08:50:46
  • 拖拽翻页(ThrowPage)详解 cool

    2009-12-02 09:54:00
  • 从Vista地址栏到网站导航菜单

    2009-09-08 12:35:00
  • Python使用xpath实现图片爬取

    2023-07-10 16:45:42
  • python函数装饰器用法实例详解

    2023-09-23 09:40:16
  • JavaScript 自动分号插入(JavaScript synat:auto semicolon insertion)

    2013-08-09 10:14:56
  • SQL Server 2005中数据库镜像的四个问题

    2009-02-19 16:48:00
  • 功能和外观都还不错的js版幻灯片效果

    2007-08-05 12:20:00
  • 精简高效的CSS命名准则和方法

    2010-09-17 18:38:00
  • Python调用钉钉自定义机器人的实现

    2023-08-29 20:08:55
  • CSS技巧及常见问题列表

    2008-04-06 14:00:00
  • MySQL修改默认字符集

    2010-11-02 12:11:00
  • 用CSS定义 li 样式

    2007-09-28 20:56:00
  • 利用Python实现Json序列化库的方法步骤

    2023-01-15 07:03:00
  • Python实现双向链表

    2022-06-12 17:41:34
  • Web开发者的百科全书——Google DocType

    2008-07-03 13:06:00
  • jquery 常用操作

    2010-01-12 16:00:00
  • python pandas处理excel表格数据的常用方法总结

    2023-06-02 19:49:47
  • 20个常用Python运维库和模块

    2023-05-11 21:44:13
  • asp之家 网络编程 m.aspxhome.com