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

【Pandas】pandas DataFrame infer_objects

Pandas2.2 DataFrame

Conversion

方法描述
DataFrame.astype(dtype[, copy, errors])用于将 DataFrame 中的数据转换为指定的数据类型
DataFrame.convert_dtypes([infer_objects, …])用于将 DataFrame 中的数据类型转换为更合适的类型
DataFrame.infer_objects([copy])用于尝试将 DataFrame 中的 object 类型的列转换为更具体的类型(如 int64float64boolean

pandas.DataFrame.infer_objects

pandas.DataFrame.infer_objects 是一个方法,用于尝试将 DataFrame 中的 object 类型的列转换为更具体的类型(如 int64float64boolean)。这个方法可以帮助自动推断和转换数据类型,使得数据处理更加高效和准确。

方法签名
DataFrame.infer_objects(copy=True)
参数说明
  • copy: 布尔值,默认为 True,表示是否返回一个新的 DataFrame 而不是修改原 DataFrame。
返回值
  • 返回一个新的 DataFrame,其中 object 类型的列已转换为更具体的类型。
示例

假设有一个 DataFrame 如下:

import pandas as pddata = {'A': ['1', '2', '3'],'B': ['1.1', '2.2', '3.3'],'C': ['True', 'False', 'True'],'D': ['x', 'y', 'z']
}df = pd.DataFrame(data)
print("原始 DataFrame:")
print(df)
print("\n数据类型:")
print(df.dtypes)

输出:

原始 DataFrame:A    B      C  D
0  1  1.1   True  x
1  2  2.2  False  y
2  3  3.3   True  z数据类型:
A    object
B    object
C    object
D    object
dtype: object
示例1:使用默认参数推断数据类型
df_inferred = df.infer_objects()
print("推断后的 DataFrame:")
print(df_inferred)
print("\n数据类型:")
print(df_inferred.dtypes)

结果:

推断后的 DataFrame:A    B      C  D
0  1  1.1   True  x
1  2  2.2  False  y
2  3  3.3   True  z数据类型:
A      int64
B    float64
C       bool
D     object
dtype: object
示例2:使用 copy=False 修改原 DataFrame
df.infer_objects(copy=False)
print("修改后的 DataFrame:")
print(df)
print("\n数据类型:")
print(df.dtypes)

结果:

修改后的 DataFrame:A    B      C  D
0  1  1.1   True  x
1  2  2.2  False  y
2  3  3.3   True  z数据类型:
A      int64
B    float64
C       bool
D     object
dtype: object
示例3:处理包含非数值字符串的列
data_with_non_numeric = {'A': ['1', '2', 'three'],'B': ['1.1', '2.2', '3.3'],'C': ['True', 'False', 'True'],'D': ['x', 'y', 'z']
}df_non_numeric = pd.DataFrame(data_with_non_numeric)
print("包含非数值字符串的 DataFrame:")
print(df_non_numeric)
print("\n数据类型:")
print(df_non_numeric.dtypes)df_inferred_non_numeric = df_non_numeric.infer_objects()
print("\n推断后的 DataFrame:")
print(df_inferred_non_numeric)
print("\n数据类型:")
print(df_inferred_non_numeric.dtypes)

结果:

包含非数值字符串的 DataFrame:A    B      C  D
0     1  1.1   True  x
1     2  2.2  False  y
2  three  3.3   True  z数据类型:
A    object
B    object
C    object
D    object
dtype: object推断后的 DataFrame:A    B      C  D
0     1  1.1   True  x
1     2  2.2  False  y
2  three  3.3   True  z数据类型:
A    object
B    float64
C       bool
D     object
dtype: object

通过这些示例,可以看到 pandas.DataFrame.infer_objects 方法如何尝试将 DataFrame 中的 object 类型的列转换为更具体的类型。这些方法在数据预处理和类型转换时非常有用。

注意事项
  • infer_objects 方法可以尝试将 object 类型的列转换为更具体的类型(如 int64float64boolean)。
  • 设置 copy=True 返回一个新的 DataFrame,而不会修改原 DataFrame。
  • 设置 copy=False 直接修改原 DataFrame。
  • 如果列中包含无法转换为更具体类型的值(如非数值字符串),这些列将保持 object 类型。
示例代码及验证

为了验证 pandas.DataFrame.infer_objects 方法的效果,可以运行上述示例代码并查看输出结果。

import pandas as pd# 创建一个示例 DataFrame
data = {'A': ['1', '2', '3'],'B': ['1.1', '2.2', '3.3'],'C': ['True', 'False', 'True'],'D': ['x', 'y', 'z']
}df = pd.DataFrame(data)
print("原始 DataFrame:")
print(df)
print("\n数据类型:")
print(df.dtypes)# 使用默认参数推断数据类型
df_inferred = df.infer_objects()
print("\n推断后的 DataFrame:")
print(df_inferred)
print("\n数据类型:")
print(df_inferred.dtypes)# 使用 copy=False 修改原 DataFrame
df.infer_objects(copy=False)
print("\n修改后的 DataFrame:")
print(df)
print("\n数据类型:")
print(df.dtypes)# 处理包含非数值字符串的列
data_with_non_numeric = {'A': ['1', '2', 'three'],'B': ['1.1', '2.2', '3.3'],'C': ['True', 'False', 'True'],'D': ['x', 'y', 'z']
}df_non_numeric = pd.DataFrame(data_with_non_numeric)
print("\n包含非数值字符串的 DataFrame:")
print(df_non_numeric)
print("\n数据类型:")
print(df_non_numeric.dtypes)df_inferred_non_numeric = df_non_numeric.infer_objects()
print("\n推断后的 DataFrame:")
print(df_inferred_non_numeric)
print("\n数据类型:")
print(df_inferred_non_numeric.dtypes)
运行结果

运行上述代码后,你会看到以下输出:

原始 DataFrame:A    B      C  D
0  1  1.1   True  x
1  2  2.2  False  y
2  3  3.3   True  z数据类型:
A    object
B    object
C    object
D    object
dtype: object推断后的 DataFrame:A    B      C  D
0  1  1.1   True  x
1  2  2.2  False  y
2  3  3.3   True  z数据类型:
A      int64
B    float64
C       bool
D     object
dtype: object修改后的 DataFrame:A    B      C  D
0  1  1.1   True  x
1  2  2.2  False  y
2  3  3.3   True  z数据类型:
A      int64
B    float64
C       bool
D     object
dtype: object包含非数值字符串的 DataFrame:A    B      C  D
0     1  1.1   True  x
1     2  2.2  False  y
2  three  3.3   True  z数据类型:
A    object
B    object
C    object
D    object
dtype: object推断后的 DataFrame:A    B      C  D
0     1  1.1   True  x
1     2  2.2  False  y
2  three  3.3   True  z数据类型:
A    object
B    float64
C       bool
D     object
dtype: object

通过这些示例,可以看到 pandas.DataFrame.infer_objects 方法如何尝试将 DataFrame 中的 object 类型的列转换为更具体的类型。这些方法在数据预处理和类型转换时非常有用。


http://www.mrgr.cn/news/97617.html

相关文章:

  • AnimateCC基础教学:随机抽取花名册,不能重复
  • nginx如何实现负载均衡?
  • Python 快速搭建一个小型的小行星轨道预测模型 Demo
  • 数字电子技术基础(四十)——使用Digital软件和Multisim软件模拟显示译码器
  • C++隐式转换的机制、风险与消除方法
  • Model Context Protocol(MCP)介绍
  • 机器学习 Day09 线性回归
  • 0基础 | 硬件 | LM386芯片
  • MySQL基础 [六] - 内置函数+复合查询+表的内连和外连
  • 解决MPU6050 驱动发现读取不出来姿态角度数据
  • Rust 是如何层层防错的
  • ⭐算法OJ⭐数据流的中位数【最小堆】Find Median from Data Stream
  • 《Operating System Concepts》阅读笔记:p587-p596
  • GEO, TCGA 等将被禁用?!这40个公开数据库可能要小心使用了
  • 算法刷题记录——LeetCode篇(2.7) [第161~170题](持续更新)
  • Linux下的进程管理(附加详细实验案例)
  • Android学习总结之网络篇(HTTP请求流程)
  • 【蓝桥杯】动态规划:背包问题
  • Android Input——IMS启动流程(二)
  • 每日OJ题_剑指offer数组篇(剑指offer04+剑指offer11+剑指offer21)