Python酷库之旅-第三方库Pandas(095)

news2024/9/22 5:35:40

目录

一、用法精讲

406、pandas.DataFrame.index属性

406-1、语法

406-2、参数

406-3、功能

406-4、返回值

406-5、说明

406-6、用法

406-6-1、数据准备

406-6-2、代码示例

406-6-3、结果输出

407、pandas.DataFrame.columns属性

407-1、语法

407-2、参数

407-3、功能

407-4、返回值

407-5、说明

407-6、用法

407-6-1、数据准备

407-6-2、代码示例

407-6-3、结果输出

408、pandas.DataFrame.dtypes属性

408-1、语法

408-2、参数

408-3、功能

408-4、返回值

408-5、说明

408-6、用法

408-6-1、数据准备

408-6-2、代码示例

408-6-3、结果输出

409、pandas.DataFrame.info方法

409-1、语法

409-2、参数

409-3、功能

409-4、返回值

409-5、说明

409-6、用法

409-6-1、数据准备

409-6-2、代码示例

409-6-3、结果输出

410、pandas.DataFrame.select_dtypes方法

410-1、语法

410-2、参数

410-3、功能

410-4、返回值

410-5、说明

410-6、用法

410-6-1、数据准备

410-6-2、代码示例

410-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

406、pandas.DataFrame.index属性
406-1、语法
# 406、pandas.DataFrame.index属性
pandas.DataFrame.index
The index (row labels) of the DataFrame.

The index of a DataFrame is a series of labels that identify each row. The labels can be integers, strings, or any other hashable type. The index is used for label-based access and alignment, and can be accessed or modified using this attribute.

Returns:
pandas.Index
The index labels of the DataFrame.
406-2、参数

        无

406-3、功能

        用于访问或设置DataFrame的索引,索引在DataFrame中扮演着重要角色,因为它定义了行的标签,以便于数据的检索和操作。

406-4、返回值

        返回一个Index对象,表示DataFrame的行索引,该对象可以包含多种类型,比如整数、字符串、时间戳等。

406-5、说明

        无

406-6、用法
406-6-1、数据准备
406-6-2、代码示例
# 406、pandas.DataFrame.index属性
import pandas as pd
# 创建一个DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
# 访问索引
index = df.index
print("索引为:", index)
# 设置新的索引
df.set_index('A', inplace=True)
print("新的索引为:", df.index)
# 重置索引
df.reset_index(inplace=True)
print("重置后的索引为:", df.index) 
406-6-3、结果输出
# 406、pandas.DataFrame.index属性
# 索引为: RangeIndex(start=0, stop=3, step=1)
# 新的索引为: Index([1, 2, 3], dtype='int64', name='A')
# 重置后的索引为: RangeIndex(start=0, stop=3, step=1)
407、pandas.DataFrame.columns属性
407-1、语法
# 407、pandas.DataFrame.columns属性
pandas.DataFrame.columns
The column labels of the DataFrame.
407-2、参数

        无

407-3、功能

        用于获取或设置DataFrame的列标签(列名)。

407-4、返回值

        返回一个Index对象,表示DataFrame的列标签,它可以包含各种类型的数据,如字符串、日期等。

407-5、说明

        无

407-6、用法
407-6-1、数据准备
407-6-2、代码示例
# 407、pandas.DataFrame.columns属性
import pandas as pd
# 创建一个DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
# 访问列名
columns = df.columns
print("列名为:", columns)
# 设置新的列名
df.columns = ['Column1', 'Column2']
print("新的列名为:", df.columns)
407-6-3、结果输出
# 407、pandas.DataFrame.columns属性
# 列名为: Index(['A', 'B'], dtype='object')
# 新的列名为: Index(['Column1', 'Column2'], dtype='object')
408、pandas.DataFrame.dtypes属性
408-1、语法
# 408、pandas.DataFrame.dtypes属性
pandas.DataFrame.dtypes
Return the dtypes in the DataFrame.

This returns a Series with the data type of each column. The result’s index is the original DataFrame’s columns. Columns with mixed types are stored with the object dtype. See the User Guide for more.

Returns:
pandas.Series
The data type of each column.
408-2、参数

        无

408-3、功能

        用于获取DataFrame中每一列的数据类型,该属性是一个Series,其索引为列名,值为对应列的数据类型。

408-4、返回值

        返回一个Series,索引是列名,值的数据类型是(int64,float64,object,datetime64[ns]等)。

408-5、说明

        无

408-6、用法
408-6-1、数据准备
408-6-2、代码示例
# 408、pandas.DataFrame.dtypes属性
import pandas as pd
# 创建一个DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4.5, 5.5, 6.5],
    'C': ['foo', 'bar', 'baz'],
    'D': pd.date_range('20240101', periods=3)
}
df = pd.DataFrame(data)
# 查看每一列的数据类型
dtypes = df.dtypes
print("每列的数据类型:")
print(dtypes)
408-6-3、结果输出
# 408、pandas.DataFrame.dtypes属性
# 每列的数据类型:
# A             int64
# B           float64
# C            object
# D    datetime64[ns]
# dtype: object
409、pandas.DataFrame.info方法
409-1、语法
# 409、pandas.DataFrame.info方法
pandas.DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)
Print a concise summary of a DataFrame.

This method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage.

Parameters:
verbosebool, optional
Whether to print the full summary. By default, the setting in pandas.options.display.max_info_columns is followed.

bufwritable buffer, defaults to sys.stdout
Where to send the output. By default, the output is printed to sys.stdout. Pass a writable buffer if you need to further process the output.

max_colsint, optional
When to switch from the verbose to the truncated output. If the DataFrame has more than max_cols columns, the truncated output is used. By default, the setting in pandas.options.display.max_info_columns is used.

memory_usagebool, str, optional
Specifies whether total memory usage of the DataFrame elements (including the index) should be displayed. By default, this follows the pandas.options.display.memory_usage setting.

True always show memory usage. False never shows memory usage. A value of ‘deep’ is equivalent to “True with deep introspection”. Memory usage is shown in human-readable units (base-2 representation). Without deep introspection a memory estimation is made based in column dtype and number of rows assuming values consume the same memory amount for corresponding dtypes. With deep memory introspection, a real memory usage calculation is performed at the cost of computational resources. See the Frequently Asked Questions for more details.

show_countsbool, optional
Whether to show the non-null counts. By default, this is shown only if the DataFrame is smaller than pandas.options.display.max_info_rows and pandas.options.display.max_info_columns. A value of True always shows the counts, and False never shows the counts.

Returns:
None
This method prints a summary of a DataFrame and returns None.
409-2、参数

409-2-1、verbose(可选,默认值为None)布尔值,是否详细显示所有列的信息,True会显示所有列的信息,False则只显示前几列,None会根据列数自动决定。

409-2-2、buf(可选,默认值为None)TextIOBase,如果指定,则输出信息会写入到这个对象中,而不是直接打印到屏幕上,通常用于重定向输出到文件或其他地方。

409-2-3、max_cols(可选,默认值为None)整数,指定在verbose=True时要显示的最大列数,如果列数超过这个限制,超出的列会以省略号表示。

409-2-4、memory_usage(可选,默认值为None)布尔值或'deep',如果为True,则计算DataFrame的内存使用情况并显示;如果为'deep',则进行更深层次的内存使用估算,适用于包含对象类型(如字符串)的列;None时,根据DataFrame的大小自动决定。

409-2-5、show_counts(可选,默认值为None)布尔值,当为True时,显示非空计数;如果为None,则根据列的数量决定是否显示计数。

409-3、功能

        用于快速获取DataFrame的概述信息,包括数据类型、缺失值、内存使用情况等,这对数据的初步探索和理解非常有帮助。

409-4、返回值

        该方法不返回任何值(返回None),但会打印出DataFrame的详细信息。

409-5、说明

        无

409-6、用法
409-6-1、数据准备
409-6-2、代码示例
# 409、pandas.DataFrame.info方法
import pandas as pd
# 创建一个DataFrame
data = {
    'A': [1, 2, None],
    'B': [4.5, None, 6.5],
    'C': ['foo', 'bar', 'baz']
}
df = pd.DataFrame(data)
# 使用info()方法查看DataFrame的信息
df.info(verbose=True, memory_usage=True, show_counts=True)
409-6-3、结果输出
# 409、pandas.DataFrame.info方法
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 3 entries, 0 to 2
# Data columns (total 3 columns):
#  #   Column  Non-Null Count  Dtype
# ---  ------  --------------  -----
#  0   A       2 non-null      float64
#  1   B       2 non-null      float64
#  2   C       3 non-null      object
# dtypes: float64(2), object(1)
# memory usage: 204.0+ bytes
410、pandas.DataFrame.select_dtypes方法
410-1、语法
# 410、pandas.DataFrame.select_dtypes方法
pandas.DataFrame.select_dtypes(include=None, exclude=None)
Return a subset of the DataFrame’s columns based on the column dtypes.

Parameters:
include, exclude
scalar or list-like
A selection of dtypes or strings to be included/excluded. At least one of these parameters must be supplied.

Returns:
DataFrame
The subset of the frame including the dtypes in include and excluding the dtypes in exclude.

Raises:
ValueError
If both of include and exclude are empty

If include and exclude have overlapping elements

If any kind of string dtype is passed in.
410-2、参数

410-2-1、include(可选,默认值为None)字符串、列表或None,指定要包括的数据类型。可以是以下值之一:

  • float:浮点型数据
  • int:整型数据
  • object:对象类型(通常是字符串)
  • string:字符串类型(在Pandas 1.0及以上版本)
  • boolean:布尔型数据
  • category:类别数据
  • 还可以通过列表同时包含多个类型,如['float','int']。

410-2-2、exclude(可选,默认值为None)字符串、列表或None,指定要排除的数据类型,用法与include类似,可以排除一种或多种数据类型。

410-3、功能

        通过列的数据类型来选择DataFrame中的列,这样可以方便地处理特定类型的数据,可以灵活地组合include和exclude,从而精确地控制要返回的列。

410-4、返回值

        返回一个新的DataFrame,包含满足条件的列,这个新DataFrame仅包含指定的数据类型,原始DataFrame不会受到影响。

410-5、说明

        无

410-6、用法
410-6-1、数据准备
410-6-2、代码示例
# 410、pandas.DataFrame.select_dtypes方法
import pandas as pd
# 创建一个DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4.5, 5.5, 6.5],
    'C': ['foo', 'bar', 'baz'],
    'D': [True, False, True]
}
df = pd.DataFrame(data)
# 选择所有浮点和整型数据的列
numeric_df = df.select_dtypes(include=['float', 'int'])
# 选择所有字符串类型的列
string_df = df.select_dtypes(include='object')
# 排除布尔型数据的列
non_bool_df = df.select_dtypes(exclude='boolean')
print("Numeric DataFrame:")
print(numeric_df)
print("\nString DataFrame:")
print(string_df)
print("\nNon-Boolean DataFrame:")
print(non_bool_df)
410-6-3、结果输出
# 410、pandas.DataFrame.select_dtypes方法
# Numeric DataFrame:
#    A    B
# 0  1  4.5
# 1  2  5.5
# 2  3  6.5
#
# String DataFrame:
#      C
# 0  foo
# 1  bar
# 2  baz
#
# Non-Boolean DataFrame:
#    A    B    C
# 0  1  4.5  foo
# 1  2  5.5  bar
# 2  3  6.5  baz

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2069888.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

楼顶气膜羽毛球馆:城市健身新空间—轻空间

随着城市化进程的加快&#xff0c;城市土地资源愈发紧张&#xff0c;如何高效利用有限的空间成为一大挑战。楼顶气膜羽毛球馆作为一种创新的体育场馆建设方式&#xff0c;凭借其独特的优势&#xff0c;逐渐成为城市健身的新宠。它不仅有效利用了楼顶闲置空间&#xff0c;还为市…

新160个crackme - 039-eKH.1

运行分析 需要破解Name和Serial&#xff0c;写出注册机 PE分析 - Delphi程序&#xff0c;32位&#xff0c;无壳 静态分析&动态调试 ida搜索关键字符串&#xff0c;跳转到关键代码 静态分析&#xff0c;修改变量如上&#xff0c;关键在于sub_427A20函数返回值需要大于等于1…

“双指针”算法下篇

WeChat_20240806081335 对双指针这一思想在OJ 里面的相关应用&#xff0c;感兴趣的友友们&#xff0c;可以看下此篇博客 https://blog.csdn.net/X_do_myself/article/details/141291451?spm1001.2014.3001.5502 目录 一盛最多水的容器 1题目链接&#xff1a;盛最多水的容器…

EmguCV学习笔记 VB.Net 6.5 凸包和凸缺陷

版权声明&#xff1a;本文为博主原创文章&#xff0c;转载请在显著位置标明本文出处以及作者网名&#xff0c;未经作者允许不得用于商业目的。 EmguCV是一个基于OpenCV的开源免费的跨平台计算机视觉库,它向C#和VB.NET开发者提供了OpenCV库的大部分功能。 教程VB.net版本请访问…

sgsegse

c语言中的小小白-CSDN博客c语言中的小小白关注算法,c,c语言,贪心算法,链表,mysql,动态规划,后端,线性回归,数据结构,排序算法领域.https://blog.csdn.net/bhbcdxb123?spm1001.2014.3001.5343 给大家分享一句我很喜欢我话&#xff1a; 知不足而奋进&#xff0c;望远山而前行&am…

货车制造5G智能工厂工业物联数字孪生平台,推进制造业数字化转型

货车制造5G智能工厂工业物联数字孪生平台&#xff0c;推进制造业数字化转型。随着5G技术的飞速发展与工业物联网的深度融合&#xff0c;货车制造5G智能工厂工业物联数字孪生平台应运而生&#xff0c;它不仅重新定义了生产模式&#xff0c;更以强大的技术驱动力&#xff0c;推动…

开放世界目标检测:检测区分出未知物体

开放世界目标检测&#xff1a;检测区分出未知物体 01 Abstract 开放世界目标检测旨在识别未见过类别的目标&#xff0c;并在提供注释后逐步识别这些目标。与传统的只限于预定义类别的范式不同&#xff0c;这种设置承诺通过使用与类别无关的信息来持续且通用地估计目标性。然而…

Java码农35岁之后只能送外卖?

声明&#xff1a;此篇为 ai123.cn 原创文章&#xff0c;转载请标明出处链接&#xff1a;https://ai123.cn/2208.html Hey&#xff0c;Java界的小伙伴们&#xff0c;有没有感受到互联网行业这一场没有硝烟的“代际战争”&#xff1f;&#x1f916;&#x1f4a5; 关于“35岁后只…

配电房挂轨机器人巡检系统的主要优点包括

背景 配电房是724h工作的封闭环境&#xff0c;人工巡检无法在时间上和空间上对配电室进行全量监控。有限的巡检时间&#xff0c;必然带来设备运转的黑盒时间&#xff0c;设备故障和隐患无法及时监控与消缺。因而不可避免存在漏检、误检的情况&#xff0c;不仅容易隐藏电力系统…

AI Agent产品经理血泪史:一年来我摸过的那些石头【Tools篇】

前几天刚好看到一篇关于GPT-6的报道&#xff0c;才想起来还有这麽回事情&#xff0c;于是赶紧把草稿捞出来改改交个任务。 至于为什麽贴这张图&#xff0c;以及为什麽血泪史从Tools开篇。 那是因为你看&#xff0c;即使到了GPT-6的时代&#xff0c;Tools仍然是AI Agent落地的…

ElementPlus下拉框输入框对齐问题

1.问题 2.解决方法 2.1label-width 说明&#xff1a;el-form中label-width设置为auto 2.2 label-wdith固定值 说明&#xff1a;如果在el-form-item里面设置了label-width"100px"&#xff1b;采用宫格布局。 .demo-one{display: grid;grid-template-columns: repe…

C++:vector篇

前言&#xff1a; 本篇仅介绍vector中常用的函数接口&#xff0c;如果需要详细的请到官网查看。 vector是一种动态数组&#xff0c;能够自动调整大小。与数组类似&#xff0c;vector使用连续内存来存储元素&#xff0c;允许高效访问&#xff0c;但可以动态增加容量。为了应对容…

买了服务器后如何正确挂载数据盘|什么是系统盘,什么是数据盘

一、前言 我们买了服务器后&#xff0c;一般会再买一个数据盘&#xff0c;如果没有数据盘&#xff0c;万一服务器系统出现问题后数据丢失就完了&#xff0c;什么数据都没了&#xff0c;所以为了避免意外的发生&#xff0c;我们通常会再买一个数据盘 如上图&#xff0c;我就在…

差一点通关某公司面试靶场

没有web4和web8&#xff0c;web2的4没做出来。 web1 国光ssrf改版靶场 1 直接读取flag 3 使用file协议读取hosts IP为172.18.240.5 dict探针内网主机开启常见端口 172.18.240.1:80 172.18.240.5:3306 172.18.240.1:3306 172.18.240.5:80 172.18.240.5:8080 172.18.240.1:…

RocketMQ Dashboard

rocketmq-dashboard是一个可视化查看和管理RocketMQ消息队列的工具 官方地址&#xff1a;RocketMQ Dashboard | RocketMQ 1、点击下载源码 2、下载并解压&#xff0c;切换至源码目录rocketmq-dashboard-1.0.0 3、修改配置文件 4、编译 rocketmq-dashboard打成jar包 &#xf…

【2024最新】注册Github账号图文教程

GitHub 是一个全球最大的开源代码托管平台&#xff0c;它提供了基于 Git 的版本控制和协作功能&#xff0c;使开发者能够共享、管理和协作开发项目。用户可以创建、克隆、编辑代码&#xff0c;并通过分支、合并请求等工具进行协同工作。GitHub 还提供社区交流、项目管理和代码审…

qt圆环饼状图,非常小的窗口都能显示

非常小的窗口都能显示 QT core gui charts#include <QtCharts> using namespace QtCharts;//创建饼状图 void MainWindow::createpieSewies() {//饼状图QPieSeries * my_pieSeries new QPieSeries();//中间圆与大圆的比例my_pieSeries->setHoleSize(0.35);//…

Spring MVC、Spring Boot和Spring Cloud

一、Spring MVC 主要特点 传统的基于Servlet的Web框架: 需要手动配置Servlet、Filter等。 配置灵活: 可以使用XML或Java类来定义Bean和依赖关系。 依赖于Web容器: 需要部署到外部Web容器&#xff08;如Tomcat、Jetty&#xff09;中运行。 视图技术支持: 支持JSP、Thymelea…

DALI-2 NFC调光解码方案,电源模块,解码板

DALI-2 DT6 NFC 调光模块 一、产品概述 深圳锐科光电科技有限公司的DALI2 DT6&D4i&#xff0c;NFC调光模块&#xff0c;符合IEC62386-101 Ed2.0、IEC62386-102 Ed2.0和 IEC62386-207 Ed1 adapted to Ed2&#xff08;DT6&#xff09;标准协议。采用国外进口单片机芯片&#…

阮一峰《TypeScript 教程》学习笔记一类型系统、数组、元祖

阮一峰《TypeScript 教程》 本篇文章主要记录浏览阮一峰《TypeScript 教程》书籍的过程中本人不会的一些TypeScript的用法。当然&#xff0c;可以说&#xff0c;我都不会哈哈哈&#xff0c;不过有的用法比较奇葩的我就不记录了&#xff0c;只记录我觉得项目中会用到&#xff0c…