【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 类型的列转换为更具体的类型(如 int64 、float64 或 boolean ) |
pandas.DataFrame.infer_objects
pandas.DataFrame.infer_objects
是一个方法,用于尝试将 DataFrame 中的 object
类型的列转换为更具体的类型(如 int64
、float64
或 boolean
)。这个方法可以帮助自动推断和转换数据类型,使得数据处理更加高效和准确。
方法签名
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
类型的列转换为更具体的类型(如int64
、float64
或boolean
)。- 设置
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
类型的列转换为更具体的类型。这些方法在数据预处理和类型转换时非常有用。