热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何从Pandas中的value_counts()中提取值名和计数?

如何从Pandas中的value_counts()中提取值名和计数?

如何从 Pandas 中的 value_counts()中提取值名和计数?

原文:https://www . geeksforgeeks . org/如何从熊猫的价值中提取价值名称和计数/

先决条件:


  • 熊猫

  • matplotlib

在本文中,我们将学习如何使用 values_count()从 panda 中提取名称和值。熊猫库配备了许多有用的功能,value_counts 就是其中之一。该函数返回熊猫数据帧中唯一项目的计数。

语法:


进场:



  • 导入必需模块。

  • 制作数据框

  • 使用 value_count()进行处理

  • 显示数据

示例 1: 打印列表中所有唯一的国家和第一个国家名称。

tolist() 函数返回一个值列表。

语法: Index.tolist()
参数: None
返回:列表


Python 3

import pandas as pd
import matplotlib.pyplot as plt
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
# print all unique country name in the list
su1 = df['country'].value_counts().index.tolist()
print(su1)
# print 1st unique country name present in a list
su2 = df['country'].value_counts().index.tolist()[0]
print(su2)

输出:

示例 2: 打印该列的所有唯一值和该列的第一个值。

value_count() 统计列中值的唯一出现次数

语法 : Index.value_count()
参数:
返回:该列中每个唯一值的出现次数。


Python 3

import pandas as pd
import matplotlib.pyplot as plt
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
# print country name and counts
su3 = df['country'].value_counts()
print(su3)
# print 1st country count in a list
su4 = df['country'].value_counts()[0]
print(su4)

输出:

示例 3: 使用列表中的循环打印我们的数据。

Python 3

import pandas as pd
import matplotlib.pyplot as plt
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
# printing names and count using loop.
for idx, name in enumerate(df['country'].value_counts().index.tolist()):
    print('Name :', name)
    print('Counts :', df['country'].value_counts()[idx])

输出:

示例 4: 以条形图的形式打印我们的数据。

语法:matplotlib . pyplot . plot(kind = ' ')
参数: kind:图的类型,即线、条。
返回:返回图形。


Python 3

import pandas as pd
import matplotlib.pyplot as plt
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
# Display data in a form of Graph
df['country'].value_counts().plot(kind='bar')
plt.show()

输出:


推荐阅读
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文介绍如何使用 Python 编写程序,检查给定列表中的元素是否形成交替峰值模式。我们将探讨两种不同的方法来实现这一目标,并提供详细的代码示例。 ... [详细]
  • 使用Numpy实现无外部库依赖的双线性插值图像缩放
    本文介绍如何仅使用Numpy库,通过双线性插值方法实现图像的高效缩放,避免了对OpenCV等图像处理库的依赖。文中详细解释了算法原理,并提供了完整的代码示例。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文介绍了在安装或运行 Python 项目时遇到的 'ModuleNotFoundError: No module named setuptools_rust' 错误,并提供了解决方案。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 探讨一个显示数字的故障计算器,它支持两种操作:将当前数字乘以2或减去1。本文将详细介绍如何用最少的操作次数将初始值X转换为目标值Y。 ... [详细]
author-avatar
木目心磊_559
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有