当前位置: 首页 > news >正文

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

目录

一、用法精讲

91、pandas.Series.set_flags方法

91-1、语法

91-2、参数

91-3、功能

91-4、返回值

91-5、说明

91-6、用法

91-6-1、数据准备

91-6-2、代码示例

91-6-3、结果输出

92、pandas.Series.astype方法

92-1、语法

92-2、参数

92-3、功能

92-4、返回值

92-5、说明

92-6、用法

92-6-1、数据准备

92-6-2、代码示例

92-6-3、结果输出

93、pandas.Series.convert_dtypes方法

93-1、语法

93-2、参数

93-3、功能

93-4、返回值

93-5、说明

93-6、用法

93-6-1、数据准备

93-6-2、代码示例

93-6-3、结果输出

94、pandas.Series.infer_objects方法

94-1、语法

94-2、参数

94-3、功能

94-4、返回值

94-5、说明

94-6、用法

94-6-1、数据准备

94-6-2、代码示例

94-6-3、结果输出

95、pandas.Series.copy方法

95-1、语法

95-2、参数

95-3、功能

95-4、返回值

95-5、说明

95-6、用法

95-6-1、数据准备

95-6-2、代码示例

95-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

91、pandas.Series.set_flags方法
91-1、语法
# 91、pandas.Series.set_flags方法
pandas.Series.set_flags(*, copy=False, allows_duplicate_labels=None)
Return a new object with updated flags.Parameters:
copybool, default False
Specify if a copy of the object should be made.NoteThe copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = Trueallows_duplicate_labelsbool, optional
Whether the returned object allows duplicate labels.Returns:
Series or DataFrame
The same type as the caller.
91-2、参数

91-2-1、copy(可选,默认值为False)如果设置为True,则会创建Series的一个副本,并在副本上设置标志;如果为False,则在原始Series上设置标志。大多数情况下,建议保持默认值True,以避免意外更改原始数据。

91-2-2、allows_duplicate_labels(可选,默认值为None)如果设置为True,允许Series对象包含重复的索引标签。默认情况下,Pandas的Series不允许具有相同索引标签的重复条目。

91-3、功能

        用于设置Series对象标志的方法。

91-4、返回值

        没有返回任何值,它主要用于设置Series对象的标志。

91-5、说明

        调用该方法后,会在原始Series对象上直接进行修改(如果copy参数设置为False),或者返回一个副本(如果copy参数设置为True),如果你需要获取修改后的Series对象,直接使用原始Series对象即可。

91-6、用法
91-6-1、数据准备
91-6-2、代码示例
# 91、pandas.Series.set_flags方法
import pandas as pd
# 创建一个示例Series
data = [10, 20, 30, 40]
index = ['a', 'b', 'c', 'd']
series = pd.Series(data, index=index)
# 使用set_flags方法
series_with_flags = series.set_flags(allows_duplicate_labels=True)
# 显示原Series和带有flags的Series
print("原始Series:")
print(series)
print("\n带有flags的Series:")
print(series_with_flags)
# 尝试添加重复标签,测试allows_duplicate_labels=True是否生效
series_with_duplicate_labels = series_with_flags._append(pd.Series([50], index=['a']))
print("\n带有重复标签的Series:")
print(series_with_duplicate_labels)
91-6-3、结果输出
# 91、pandas.Series.set_flags方法
# 原始Series:
# a    10
# b    20
# c    30
# d    40
# dtype: int64
# 
# 带有flags的Series:
# a    10
# b    20
# c    30
# d    40
# dtype: int64
# 
# 带有重复标签的Series:
# a    10
# b    20
# c    30
# d    40
# a    50
# dtype: int64
92、pandas.Series.astype方法
92-1、语法
# 92、pandas.Series.astype方法
pandas.Series.astype(dtype, copy=None, errors='raise')
Cast a pandas object to a specified dtype dtype.Parameters:
dtypestr, data type, Series or Mapping of column name -> data type
Use a str, numpy.dtype, pandas.ExtensionDtype or Python type to cast entire pandas object to the same type. Alternatively, use a mapping, e.g. {col: dtype, …}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame’s columns to column-specific types.copybool, default True
Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).NoteThe copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = Trueerrors{‘raise’, ‘ignore’}, default ‘raise’
Control raising of exceptions on invalid data for provided dtype.raise : allow exceptions to be raisedignore : suppress exceptions. On error return original object.Returns:
same type as caller
92-2、参数

92-2-1、dtype(必须)表示要转换为的数据类型,可以是字符串形式的数据类型名称(例如'int'、'float'、'datetime'等),也可是NumPy的数据类型对象(例如numpy.int64、numpy.float32、numpy.datetime64等)。

92-2-2、copy(可选,默认值为False)如果设置为True,则会创建Series的一个副本,并在副本上设置标志;如果为False,则在原始Series上设置标志。大多数情况下,建议保持默认值True,以避免意外更改原始数据。

92-2-3、errors(可选,默认值为'raise')如果转换过程中出现错误,应该如何处理。可以选择以下几种方式:

92-2-3-1、'raise':默认选项,如果出现错误,则引发异常。

92-2-3-2、'ignore':忽略错误,保留原始数据类型。

92-2-3-3、'coerce':将无法转换的值设置为缺失值(NaN)。

92-3、功能

        用于将Series对象的数据类型转换为指定的数据类型。

92-4、返回值

        返回一个带有转换后数据类型的新Series对象,如果copy=False,则直接在原始Series上进行数据类型转换并返回该Series。

92-5、说明

        无

92-6、用法
92-6-1、数据准备
92-6-2、代码示例
# 92、pandas.Series.astype方法
import pandas as pd
# 创建一个示例Series
data = [10, 20, 30, 40]
index = ['a', 'b', 'c', 'd']
series = pd.Series(data, index=index)
# 将数据类型转换为float,并创建副本
series_float = series.astype('float', copy=True)
# 显示转换后的Series
print(series_float, end='\n\n')
# 将数据类型转换为datetime,并在原始Series上进行转换
series_datetime = series.astype('int64', copy=False)
# 显示转换后的Series
print(series_datetime)
92-6-3、结果输出
# 92、pandas.Series.astype方法
# a    10.0
# b    20.0
# c    30.0
# d    40.0
# dtype: float64
# 
# a    10
# b    20
# c    30
# d    40
# dtype: int64
93、pandas.Series.convert_dtypes方法
93-1、语法
# 93、pandas.Series.convert_dtypes方法
pandas.Series.convert_dtypes(infer_objects=True, convert_string=True, convert_integer=True, convert_boolean=True, convert_floating=True, dtype_backend='numpy_nullable')
Convert columns to the best possible dtypes using dtypes supporting pd.NA.Parameters:
infer_objectsbool, default True
Whether object dtypes should be converted to the best possible types.convert_stringbool, default True
Whether object dtypes should be converted to StringDtype().convert_integerbool, default True
Whether, if possible, conversion can be done to integer extension types.convert_booleanbool, defaults True
Whether object dtypes should be converted to BooleanDtypes().convert_floatingbool, defaults True
Whether, if possible, conversion can be done to floating extension types. If convert_integer is also True, preference will be give to integer dtypes if the floats can be faithfully casted to integers.dtype_backend{‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’
Back-end data type applied to the resultant DataFrame (still experimental). Behaviour is as follows:"numpy_nullable": returns nullable-dtype-backed DataFrame (default)."pyarrow": returns pyarrow-backed nullable ArrowDtype DataFrame.New in version 2.0.Returns:
Series or DataFrame
Copy of input object with new dtype.
93-2、参数

93-2-1、infer_objects(可选,默认值为True)是否推断object列的类型。

93-2-2、convert_string(可选,默认值为True)是否将object类型转换为字符串。

93-2-3、convert_integer(可选,默认值为True)是否将 object类型转换为整数。

93-2-4、convert_boolean(可选,默认值为True)是否将object类型转换为布尔值。

93-2-5、convert_floating(可选,默认值为True)是否将object类型转换为浮点类型。

93-2-6、dtype_backend(可选,默认值为'numpy_nullable')内部调用,一般不需要用户设置。

93-3、功能

        旨在自动为DataFrame中的每一列或Series中的数据找到最佳的数据类型,这可以通过使用更合适的数据类型来提高内存效率和性能。

93-4、返回值

        返回值是一个新的、可能包含pandas扩展数据类型的Series,但dtype属性可能不足以完全反映这些内部优化。

93-5、说明

93-5-1、自动类型推断:该方法尝试推断Series中每个元素的最佳数据类型。例如,它可能将object类型转换为string类型,将integer转换为Int64类型,或者将float转换为Float64类型。

93-5-2、内存优化:通过转换为最合适的数据类型,可以减少内存使用。

93-5-3、可为空类型:在转换时,它还使用可为空类型(如Int64、Float64和boolean),这些类型可以比它们的不可为空对应物更优雅地处理缺失值。

93-6、用法
93-6-1、数据准备
93-6-2、代码示例
# 93、pandas.Series.convert_dtypes方法
import pandas as pd
# 创建一个包含不同类型数据的Series
data = pd.Series([1, 2.5, '3', True])
# 原始数据类型
print("原始数据类型:")
print(data.dtypes)  
# 使用 convert_dtypes 方法
converted_data = data.convert_dtypes()
# 转换后的数据类型
print("转换后的数据类型:")
print(converted_data.dtypes)
93-6-3、结果输出
# 93、pandas.Series.convert_dtypes方法
# 原始数据类型:  
# object  
# 转换后的数据类型:  
# Int64  
# Float64  
# String  
# bool  
# dtype: object
94、pandas.Series.infer_objects方法
94-1、语法
# 94、pandas.Series.infer_objects方法
pandas.Series.infer_objects(copy=None)
Attempt to infer better dtypes for object columns.Attempts soft conversion of object-dtyped columns, leaving non-object and unconvertible columns unchanged. The inference rules are the same as during normal Series/DataFrame construction.Parameters:
copybool, default True
Whether to make a copy for non-object or non-inferable columns or Series.NoteThe copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = TrueReturns:
same type as input object
94-2、参数

94-2-1、copy(可选,默认值为None)控制是否返回副本而不是在原始数据上进行操作。取值包括:

94-2-1-1、None(默认):表示方法在原始数据上进行操作,而不生成副本。

94-2-1-2、True:生成数据的副本,并在副本上执行操作,原始数据保持不变。

94-2-1-3、False:在原始数据上直接执行操作,此时原始数据可能会被修改。

94-3、功能

        尝试推断Series中的数据类型,并将其转换为更合适的数据类型,它可以帮助处理混合数据类型的情况,使得数据分析和操作更加高效。

94-4、返回值

        返回值是一个经过数据类型推断后的Pandas Series,其中每个元素的数据类型被推断为最合适的类型。

94-5、说明

        无

94-6、用法
94-6-1、数据准备
94-6-2、代码示例
# 94、pandas.Series.infer_objects方法
import pandas as pd
# 创建一个混合数据类型的Series
data = pd.Series([1, 2.0, '3', 4.5])
# 显示初始数据类型
print("初始数据类型:")
print(data.apply(type))
# 使用infer_objects方法推断数据类型
data = data.infer_objects()
# 显示推断后的数据类型
print("\n推断后的数据类型:")
print(data.apply(type))
94-6-3、结果输出
# 94、pandas.Series.infer_objects方法
# 初始数据类型:
# 0      <class 'int'>
# 1    <class 'float'>
# 2      <class 'str'>
# 3    <class 'float'>
# dtype: object
#
# 推断后的数据类型:
# 0      <class 'int'>
# 1    <class 'float'>
# 2      <class 'str'>
# 3    <class 'float'>
# dtype: object
95、pandas.Series.copy方法
95-1、语法
# 95、pandas.Series.copy方法
pandas.Series.copy(deep=True)
Make a copy of this object’s indices and data.When deep=True (default), a new object will be created with a copy of the calling object’s data and indices. Modifications to the data or indices of the copy will not be reflected in the original object (see notes below).When deep=False, a new object will be created without copying the calling object’s data or index (only references to the data and index are copied). Any changes to the data of the original will be reflected in the shallow copy (and vice versa).NoteThe deep=False behaviour as described above will change in pandas 3.0. Copy-on-Write will be enabled by default, which means that the “shallow” copy is that is returned with deep=False will still avoid making an eager copy, but changes to the data of the original will no longer be reflected in the shallow copy (or vice versa). Instead, it makes use of a lazy (deferred) copy mechanism that will copy the data only when any changes to the original or shallow copy is made.You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = TrueParameters:
deep
bool, default True
Make a deep copy, including a copy of the data and the indices. With deep=False neither the indices nor the data are copied.Returns:
Series or DataFrame
Object type matches caller.NotesWhen deep=True, data is copied but actual Python objects will not be copied recursively, only the reference to the object. This is in contrast to copy.deepcopy in the Standard Library, which recursively copies object data (see examples below).While Index objects are copied when deep=True, the underlying numpy array is not copied for performance reasons. Since Index is immutable, the underlying data can be safely shared and a copy is not needed.Since pandas is not thread safe, see the gotchas when copying in a threading environment.When copy_on_write in pandas config is set to True, the copy_on_write config takes effect even when deep=False. This means that any changes to the copied data would make a new copy of the data upon write (and vice versa). Changes made to either the original or copied variable would not be reflected in the counterpart. See Copy_on_Write for more information.
95-2、参数

95-2-1、deep(可选,默认值为True)当deep参数设置为True时,copy()方法会创建一个新的Series对象,该对象中的数据(包括其内部的numpy数组)也会被复制,这意味着新创建的Series和原始Series在内存中是完全独立的;对其中一个所做的修改(如添加、删除元素或修改元素值)不会影响到另一个。

95-3、功能

        用于创建一个Series对象的副本。

95-4、返回值

        返回一个新的Series对象。

95-5、说明

        如果使用pandas.Series.copy(deep=True),则可以进行深拷贝,这意味着不仅会复制Series的数据和索引,还会复制其中的对象引用,使得新创建的Series对象与原始对象完全独立,即使修改其中一个对象,另一个对象也不受影响,因此,deep=True参数在需要创建一个原始对象完全独立的副本时非常有用。

95-6、用法
95-6-1、数据准备
95-6-2、代码示例
# 95、pandas.Series.copy方法
import pandas as pd
# 创建一个原始的Series对象
original_series = pd.Series([1, 2, 3, 4, 5])
# 进行深拷贝,创建副本
copied_series = original_series.copy(deep=True)
# 修改原始对象的值
original_series[0] = 100
# 打印两个对象,观察副本是否受到影响
print("原始Series对象:")
print(original_series)
print("\n拷贝的Series对象:")
print(copied_series)
95-6-3、结果输出
# 95、pandas.Series.copy方法
# 原始Series对象:
# 0    100
# 1      2
# 2      3
# 3      4
# 4      5
# dtype: int64
#
# 拷贝的Series对象:
# 0    1
# 1    2
# 2    3
# 3    4
# 4    5
# dtype: int64

二、推荐阅读

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 食堂采购系统开发:从需求分析到上线实施的完整指南
  • npm install时报错 reason: connect ETIMEDOUT
  • 网络安全-等级保护制度介绍
  • 使用AJAX发起一个异步请求,从【api_endpoint】获取数据,并在成功时更新页面上的【target_element】
  • 详细分析Java中的6种请求方式(附Demo)
  • WPF TreeView 全选/反选/子级选中父级也选中
  • 降低芯片流片风险的几种方法
  • 算法第十天:leetcode203.移除链表元素
  • 【C++】C++11的新特性 --- 右值引用与移动语义
  • 排序系列 之 选择排序
  • FastAPI -- 第三弹(自定义响应、中间件、代理、WebSockets)
  • 【Python 对接QQ的接口(三)】简单用接口查询【等级/昵称/头像/Q龄/状态/会员/当天在线时长/下一个等级升级需多少天】
  • 架构以及架构中的组件
  • 迪米特法则
  • leetcode145. 二叉树的后序遍历,递归法+迭代法,全过程图解+步步解析,一点点教会你迭代法后序遍历
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • “大数据应用场景”之隔壁老王(连载四)
  • 【391天】每日项目总结系列128(2018.03.03)
  • Phpstorm怎样批量删除空行?
  • Service Worker
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • Theano - 导数
  • Unix命令
  • 关于字符编码你应该知道的事情
  • 欢迎参加第二届中国游戏开发者大会
  • 利用jquery编写加法运算验证码
  • 每天10道Java面试题,跟我走,offer有!
  • 如何利用MongoDB打造TOP榜小程序
  • 算法-图和图算法
  • 用 Swift 编写面向协议的视图
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​经​纬​恒​润​二​面​​三​七​互​娱​一​面​​元​象​二​面​
  • ​十个常见的 Python 脚本 (详细介绍 + 代码举例)
  • ###STL(标准模板库)
  • #Linux(Source Insight安装及工程建立)
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • $jQuery 重写Alert样式方法
  • (06)Hive——正则表达式
  • (Java数据结构)ArrayList
  • (STM32笔记)九、RCC时钟树与时钟 第二部分
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (二)Kafka离线安装 - Zookeeper下载及安装
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • (四)stm32之通信协议
  • (一)VirtualBox安装增强功能
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (转)h264中avc和flv数据的解析
  • (转)原始图像数据和PDF中的图像数据