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

目录

一、用法精讲

551、pandas.DataFrame.notna方法

551-1、语法

551-2、参数

551-3、功能

551-4、返回值

551-5、说明

551-6、用法

551-6-1、数据准备

551-6-2、代码示例

551-6-3、结果输出

552、pandas.DataFrame.notnull方法

552-1、语法

552-2、参数

552-3、功能

552-4、返回值

552-5、说明

552-6、用法

552-6-1、数据准备

552-6-2、代码示例

552-6-3、结果输出

553、pandas.DataFrame.pad方法

553-1、语法

553-2、参数

553-3、功能

553-4、返回值

553-5、说明

553-6、用法

553-6-1、数据准备

553-6-2、代码示例

553-6-3、结果输出

554、pandas.DataFrame.replace方法

554-1、语法

554-2、参数

554-3、功能

554-4、返回值

554-5、说明

554-6、用法

554-6-1、数据准备

554-6-2、代码示例

554-6-3、结果输出

555、pandas.DataFrame.droplevel方法

555-1、语法

555-2、参数

555-3、功能

555-4、返回值

555-5、说明

555-6、用法

555-6-1、数据准备

555-6-2、代码示例

555-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

551、pandas.DataFrame.notna方法
551-1、语法
# 551、pandas.DataFrame.notna方法
pandas.DataFrame.notna()
Detect existing (non-missing) values.Return a boolean same-sized object indicating if the values are not NA. Non-missing values get mapped to True. Characters such as empty strings '' or numpy.inf are not considered NA values (unless you set pandas.options.mode.use_inf_as_na = True). NA values, such as None or numpy.NaN, get mapped to False values.Returns:
DataFrame
Mask of bool values for each element in DataFrame that indicates whether an element is not an NA value.
551-2、参数

        无

551-3、功能

        该方法逐元素检查DataFrame,判断每个元素是否为缺失值。

551-4、返回值

        返回一个与原DataFrame形状相同的布尔DataFrame,其中每个元素对应着原DataFrame中元素的非缺失状态。

551-5、说明

        无

551-6、用法
551-6-1、数据准备
551-6-2、代码示例
# 551、pandas.DataFrame.notna方法
import pandas as pd
import numpy as np
# 创建示例DataFrame
data = {'A': [1, 2, np.nan],'B': [np.nan, 3, 4],'C': [5, np.nan, 6]
}
df = pd.DataFrame(data)
# 使用notna()方法
not_na_df = df.notna()
print(not_na_df)
551-6-3、结果输出
# 551、pandas.DataFrame.notna方法
#        A      B      C
# 0   True  False   True
# 1   True   True  False
# 2  False   True   True
552、pandas.DataFrame.notnull方法
552-1、语法
# 552、pandas.DataFrame.notnull方法
pandas.DataFrame.notnull()
DataFrame.notnull is an alias for DataFrame.notna.Detect existing (non-missing) values.Return a boolean same-sized object indicating if the values are not NA. Non-missing values get mapped to True. Characters such as empty strings '' or numpy.inf are not considered NA values (unless you set pandas.options.mode.use_inf_as_na = True). NA values, such as None or numpy.NaN, get mapped to False values.Returns:
DataFrame
Mask of bool values for each element in DataFrame that indicates whether an element is not an NA value.
552-2、参数

        无

552-3、功能

        该方法逐元素检查DataFrame,判断每个元素是否为非缺失值。

552-4、返回值

        返回一个与原DataFrame形状相同的布尔DataFrame,对于每个位置:

  • True:表示该位置的值是有效值(非缺失值)。
  • False:表示该位置的值是缺失值(如NaN)。
552-5、说明

        无

552-6、用法
552-6-1、数据准备
552-6-2、代码示例
# 552、pandas.DataFrame.notnull方法
import pandas as pd
import numpy as np
# 创建示例DataFrame
data = {'A': [1, 2, np.nan],'B': [np.nan, 3, 4],'C': [5, np.nan, 6]
}
df = pd.DataFrame(data)
# 使用notnull()方法
not_null_df = df.notnull()
print(not_null_df)
552-6-3、结果输出
# 552、pandas.DataFrame.notnull方法
#        A      B      C
# 0   True  False   True
# 1   True   True  False
# 2  False   True   True
553、pandas.DataFrame.pad方法
553-1、语法
# 553、pandas.DataFrame.pad方法
pandas.DataFrame.pad(*, axis=None, inplace=False, limit=None, downcast=_NoDefault.no_default)
Fill NA/NaN values by propagating the last valid observation to next valid.Deprecated since version 2.0: Series/DataFrame.pad is deprecated. Use Series/DataFrame.ffill instead.Returns:
Series/DataFrame or None
Object with missing values filled or None if inplace=True.
553-2、参数

553-2-1、axis(可选,默认值为None){0 or 'index', 1 or 'columns'},指定填充的方向:

  • 0或'index':进行按行填充。
  • 1或'columns':进行按列填充。
  • None:默认情况下按行填充。

553-2-2、inplace(可选,默认值为False)布尔值,如果为True,则直接在原DataFrame上进行填充,不返回新的对象;如果为False,则返回填充后的新DataFrame,而不改变原始DataFrame。

553-2-3、limit(可选,默认值为None)整数,指定每列或每行能填充的缺失值的最大数量,如果没有设置,则会填充所有缺失值。

553-2-4、downcast(可选)字符串,用于在填充过程中将结果的类型向下转换(例如将浮点数转换为整数等),可以指定想要的类型。

553-3、功能

        用于在DataFrame中填充缺失值的方法,该方法可以通过向前填充或向后一致地填充缺失值,以便用已知的值替代缺失值。

553-4、返回值

        返回一个新的DataFrame(如果inplace=False)或对原DataFrame进行修改(如果inplace=True),填充了缺失值,返回的DataFrame保留了原始DataFrame的索引和列标签。

553-5、说明

        无

553-6、用法
553-6-1、数据准备
553-6-2、代码示例
# 553、pandas.DataFrame.pad方法
import pandas as pd
import numpy as np
# 创建示例DataFrame
data = {'A': [1, np.nan, 3],'B': [4, 5, np.nan],'C': [np.nan, np.nan, 6]
}
df = pd.DataFrame(data)
# 使用pad方法向前填充缺失值
filled_df = df.pad()
print(filled_df)
553-6-3、结果输出
# 553、pandas.DataFrame.pad方法
#      A    B    C
# 0  1.0  4.0  NaN
# 1  1.0  5.0  NaN
# 2  3.0  5.0  6.0
554、pandas.DataFrame.replace方法
554-1、语法
# 554、pandas.DataFrame.replace方法
pandas.DataFrame.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)
Replace values given in to_replace with value.Values of the Series/DataFrame are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value.Parameters:
to_replacestr, regex, list, dict, Series, int, float, or None
How to find the values that will be replaced.numeric, str or regex:numeric: numeric values equal to to_replace will be replaced with valuestr: string exactly matching to_replace will be replaced with valueregex: regexs matching to_replace will be replaced with valuelist of str, regex, or numeric:First, if to_replace and value are both lists, they must be the same length.Second, if regex=True then all of the strings in both lists will be interpreted as regexs otherwise they will match directly. This doesn’t matter much for value since there are only a few possible substitution regexes you can use.str, regex and numeric rules apply as above.dict:Dicts can be used to specify different replacement values for different existing values. For example, {'a': 'b', 'y': 'z'} replaces the value ‘a’ with ‘b’ and ‘y’ with ‘z’. To use a dict in this way, the optional value parameter should not be given.For a DataFrame a dict can specify that different values should be replaced in different columns. For example, {'a': 1, 'b': 'z'} looks for the value 1 in column ‘a’ and the value ‘z’ in column ‘b’ and replaces these values with whatever is specified in value. The value parameter should not be None in this case. You can treat this as a special case of passing two lists except that you are specifying the column to search in.For a DataFrame nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with NaN. The optional value parameter should not be specified to use a nested dict in this way. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions.None:This means that the regex argument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. If value is also None then this must be a nested dictionary or Series.See the examples section for examples of each of these.valuescalar, dict, list, str, regex, default None
Value to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed.inplacebool, default False
If True, performs operation inplace and returns None.limitint, default None
Maximum size gap to forward or backward fill.Deprecated since version 2.1.0.regexbool or same types as to_replace, default False
Whether to interpret to_replace and/or value as regular expressions. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None.method{‘pad’, ‘ffill’, ‘bfill’}
The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.Deprecated since version 2.1.0.Returns:
Series/DataFrame
Object after replacement.Raises:
AssertionError
If regex is not a bool and to_replace is not None.TypeError
If to_replace is not a scalar, array-like, dict, or NoneIf to_replace is a dict and value is not a list, dict, ndarray, or SeriesIf to_replace is None and regex is not compilable into a regular expression or is a list, dict, ndarray, or Series.When replacing multiple bool or datetime64 objects and the arguments to to_replace does not match the type of the value being replacedValueError
If a list or an ndarray is passed to to_replace and value but they are not the same length.
554-2、参数

554-2-1、to_replace(可选,默认值为None)scalar, list-like, dict或正则表达式,指要被替换的值,可以是单个值、值的列表或者一个字典(键为旧值,值为新值),也可以是正则表达式。

554-2-2、value(可选)scalar, list-like或 dict,表示新的值,用于替换to_replace中对应的值,如果to_replace是一个字典,则value也应是一个字典,且键值对应。

554-2-3、inplace(可选,默认值为False)布尔值,如果为True,则直接在原DataFrame上进行替换,不返回新的对象;如果为False,则返回替换后的新DataFrame,而不改变原始DataFrame。

554-2-4、limit(可选,默认值为None)整数,指定最大替换次数,如果未设置,则会替换所有符合条件的值。

554-2-5、regex(可选,默认值为False)布尔值,如果为True,to_replace将被视为正则表达式,并根据正则表达式的匹配进行替换。

554-2-6、method(可选)字符串,该参数仅在使用to_replace为字典时有效,可以指定替换的方式,例如'pad'或'ffill'。

554-3、功能

        用于替换DataFrame中指定值的方法,该方法可以用新的值替换旧的值,支持多种替换方式,如逐元素替换、使用正则表达式等。

554-4、返回值

        返回一个新的DataFrame(如果inplace=False)或对原DataFrame进行修改(如果inplace=True),替换了指定的值,返回的DataFrame保留了原始DataFrame的索引和列标签。

554-5、说明

        无

554-6、用法
554-6-1、数据准备
554-6-2、代码示例
# 554、pandas.DataFrame.replace方法
import pandas as pd
# 创建示例DataFrame
data = {'A': [1, 2, 3],'B': ['apple', 'banana', 'apple'],'C': [3.5, 4.5, 5.5]
}
df = pd.DataFrame(data)
# 使用replace方法替换值
df_replaced = df.replace({'apple': 'orange', 2: 20})
print(df_replaced)
554-6-3、结果输出
# 554、pandas.DataFrame.replace方法
#     A       B    C
# 0   1  orange  3.5
# 1  20  banana  4.5
# 2   3  orange  5.5
555、pandas.DataFrame.droplevel方法
555-1、语法
# 555、pandas.DataFrame.droplevel方法
pandas.DataFrame.droplevel(level, axis=0)
Return Series/DataFrame with requested index / column level(s) removed.Parameters:
levelint, str, or list-like
If a string is given, must be the name of a level If list-like, elements must be names or positional indexes of levels.axis{0 or ‘index’, 1 or ‘columns’}, default 0
Axis along which the level(s) is removed:0 or ‘index’: remove level(s) in column.1 or ‘columns’: remove level(s) in row.For Series this parameter is unused and defaults to 0.Returns:
Series/DataFrame
Series/DataFrame with requested index / column level(s) removed.
555-2、参数

555-2-1、level(必须)int, str或者list,指定要删除的级别,可以是:

  • 级别的整数位置(例如0表示第一个级别)
  • 级别的名称(如果索引有命名的话)
  • 由上述两种类型构成的列表,用于同时删除多个级别。

555-2-2、axis(可选,默认值为0){0 or 'index', 1 or 'columns'},指定填充的方向:

  • 0或'index':进行按行填充。
  • 1或'columns':进行按列填充。
  • None:默认情况下按行填充。
555-3、功能

        从指定的MultiIndex中删除给定级别,这样可以简化数据框的索引结构。当删除级别时,其他级别的信息将被保留,最终的DataFrame仍将是一个有效的pandas DataFrame,只是索引的结构发生了变化。

555-4、返回值

        返回一个新的DataFrame,索引或列索引中指定级别被删除后形成的新DataFrame,原始DataFrame不会被修改。

555-5、说明

        无

555-6、用法
555-6-1、数据准备
555-6-2、代码示例
# 555、pandas.DataFrame.droplevel方法
import pandas as pd
# 创建一个MultiIndex DataFrame
arrays = [['A', 'A', 'B', 'B'],['one', 'two', 'one', 'two']
]
index = pd.MultiIndex.from_arrays(arrays, names=('letter', 'number'))
data = pd.DataFrame({'value': [1, 2, 3, 4]}, index=index)
# 输出原始DataFrame
print("原始DataFrame:")
print(data)
# 使用droplevel删除其中一个级别
data_dropped = data.droplevel(level='number', axis=0)
# 输出处理后的DataFrame
print("\n删除级别后的DataFrame:")
print(data_dropped)
555-6-3、结果输出
# 555、pandas.DataFrame.droplevel方法
# 原始DataFrame:
#                value
# letter number       
# A      one         1
#        two         2
# B      one         3
#        two         4
# 
# 删除级别后的DataFrame:
#         value
# letter       
# A           1
# A           2
# B           3
# B           4

二、推荐阅读

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

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

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

相关文章

为了不再被事务坑,我读透了Spring的事务传播性。

在之前文章中,我们已经被事务坑了两次: mq发送消息之后,业务代码回滚,导致发了一条中奖消息给用户!! 我又被Spring的事务坑了,用户兑奖之后,什么东西都没收到!&#xf…

【高阶用法】uniapp的i18n/修复/增强/App无重启更换语言

痛点 在i18n多语言模块使用过程中,发现下面几个问题,需要解决 1)uni-best框架下,$t功能函数无法实时的切换语言,可能跟使用有关 2)uni-best建议的translate方式在vue块外使用太繁琐,希望不用…

10年计算机考研408-计算机网络

【题33】下列选项中,不属于网络体系结构所描述的内容是() A.网络的层次 B.每一层使用的协议 C.协议的内部实现细节 D.每一层必须完成的功能 解析: 本题考查的是网络体系结构相关的概念。 图1描述了网络的7层架构以及每一层所要完成…

防火墙详解(一) 网络防火墙简介

原文链接:https://blog.csdn.net/qq_46254436/article/details/105519624 文章目录 定义 与路由器和交换机的区别 发展历史 防火墙安全区域 定义 防火墙主要用于保护一个网络区域免受来自另一个网络区域的网络攻击和网络入侵行为 “防火墙”一词起源于建筑领域&…

Openai gym environment for multi-agent games

题意:用于多智能体游戏的 OpenAI Gym 环境 问题背景: Is it possible to use openais gym environments for multi-agent games? Specifically, I would like to model a card game with four players (agents). The player scoring a turn starts the…

8月份工业机器人产量同比增长20%

近日,国家统计局公布数据显示,8月份,我国工业机器人产量为47947套,较去年同期增长20%;1-8月份,总产量为360592套,较去年同期增长9.9%。 9月14日,国家统计局发布数据显示,…

十大常用加密软件排行榜|2024年好用的加密软件推荐【精选】

在信息安全日益重要的时代,加密软件成为保护个人和企业数据的关键工具。选择合适的加密软件可以有效防止数据泄露和未授权访问。以下是2024年值得推荐的十大加密软件,帮助你找到适合的解决方案。 1. Ping32加密软件 Ping32是一款功能强大的加密软件&…

Spring Boot 学习之路 -- 处理 HTTP 请求

前言 最近因为业务需要,被拉去研究后端的项目,代码基于 Spring Boot,对我来说完全小白,需要重新学习研究…出于个人习惯,会以 Blog 文章的方式做一些记录,文章内容基本来源于「 Spring Boot 从入门到精通&…

【数列求值 / B】

题目 一般做法 #include <bits/stdc.h> using namespace std; const int mod 10000; int f[20190325] {1, 1, 1, 1}; int main() {for(int i 4; i < 20190324; i){f[i] (f[i-1] f[i-2] f[i-3]) % mod;}cout << f[20190324]; } 快速幂矩阵乘法 #includ…

索迪迈车载监控设备的优势有哪些

在当今社会&#xff0c;车载监控设备已经成为保障公共安全与交通管理的重要工具。索迪迈车载监控设备&#xff0c;以其先进的技术和卓越的性能&#xff0c;成为业界的佼佼者。其优势主要体现在以下几个方面&#xff1a; 一、抽拔式硬盘设计 1. 便捷的数据管理 车载监控设备需…

Rk628D 在 RK3588s平台上的驱动移植

硬件平台: W1_AI_RK3588S_V0 处理器: rk3588s kernel版本: Linux version 5.10.110 芯片是:rk628D 目的是:(4k)HDMI输入mipi 输出 1、下载RK628 最新(2024.09)的代码链接: 通过百度网盘分享的文件:RK628 链接:https://pan.baidu.com/s/1zN9yD2FQWAzVUMY1op…

Java面试题大全(全网最全,持续更新)初级(2)

1. 基础语法 1.1. Java 的数据类型有哪些&#xff1f; Java 有两种数据类型&#xff1a; 基本数据类型&#xff08;Primitive Types&#xff09;&#xff1a;包括 byte、short、int、long、float、double、char、boolean。引用数据类型&#xff08;Reference Types&#xff…

环境领域顶刊EST发表!又一次颠覆性突破!

2023年3月21日&#xff0c;普林斯顿大学任智勇教授团队针对最近爆火的ChatGPT和环境研究的交叉在环境领域顶级期刊《Environmental Science & Technology》发表了观点类文章“ChatGPT and Environmental Research”。 任智勇教授中对未来的展望表示&#xff1a; 颠覆性技术…

便携式气象观测仪的工作原理

TH-PQX9】便携式气象观测仪是一种集多种气象要素观测于一体&#xff0c;便于携带和使用的小型气象观测设备。实时监测和记录多种气象要素&#xff0c;包括温度、湿度、风速、风向、气压、太阳辐射、雨量等&#xff0c;满足不同场景下的气象监测需求。采用高精度传感器&#xff…

平板电容笔哪个牌子好?精选电容笔品牌排行榜前五名推荐!

在当今时代&#xff0c;平板电容笔已经成为平板电脑的重要配件&#xff0c;为人们的学习、工作和创作带来了极大的便利。然而&#xff0c;市场上平板电容笔的品牌众多&#xff0c;质量和性能也参差不齐&#xff0c;这让消费者在选择时常常感到困惑。平板电容笔究竟哪个牌子更好…

计算n个节点所能组成的不同二叉搜索树(卡特兰数)

计算n个节点所能组成的不同二叉搜索树的时候我们一般都是画图&#xff0c;但是有一个拱墅可以快速计算

概率论与数理统计(持续更新)

一.概率论基本概念 1.确定性现象与非确定性现象 确定性现象&#xff0c;具有事前可预言性 非确定性现象&#xff0c;具有事前不可预言性 2.随机现象&#xff0c;在个别实验中具有不确定性&#xff0c;在大量重复实验中呈现规律性 统计规律性&#xff0c;大量同类随机现象所…

TCP 协议机制超详解

我的主页&#xff1a;2的n次方_ 1. 协议结构 2. 确认应答 在之前提到过 TCP 的核心机制是确认应答&#xff0c;可以确认对方是否收到数据&#xff0c;在数据传输的过程中&#xff0c;如果有多条请求&#xff0c;并且返回对应的响应&#xff0c;但是此时可能会出现这样的问题…

【通俗易懂】知识图谱增强 RAG 思路 和 实现方案

【通俗易懂】知识图谱增强 RAG 思路 和 实现方案 为什么用 知识图谱增强 RAG&#xff1f;对比传统方法3 种实现方式 方案一&#xff1a;利用 KG 关系网络&#xff0c;构建问题子图促精准解答地图固定深度整体优化方案 方案二&#xff1a;利用 KG 语义关联&#xff0c;提升文档片…

【重学 MySQL】三十八、group by的使用

【重学 MySQL】三十八、group by的使用 基本语法示例示例 1: 计算每个部门的员工数示例 2: 计算每个部门的平均工资示例 3: 结合 WHERE 子句 WITH ROLLUP基本用法示例注意事项 注意事项 GROUP BY 是 SQL 中一个非常重要的子句&#xff0c;它通常与聚合函数&#xff08;如 COUNT…