Pandas DataFrame.astype()函数
Python Pandas DataFrame.astype() 函数将对象的数据类型改变为指定的数据类型。
pandas.DataFrame.astype()
语法
DataFrame.astype(dtype, copy=True, errors="raise")
参数
dtype |
我们要分配给对象的数据类型。 |
copy |
布尔参数。当 True 时返回一个副本。 |
errors |
它控制对所提供数据类型的无效数据引发异常。它有两个选项。raise :允许引发异常。ignore :抑制异常。如果存在错误,那么它将返回原始对象。 |
返回对象
它返回带有数据类型的 DataFrame。
示例代码:DataFrame.astype()
方法改变一列数据类型
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data Types of the Data frame are: \n")
print(dataframe.dtypes)
dataframe1 = dataframe.astype({'Attendance': 'int32'}).dtypes
print("The Modified Data Types of the Data frame are: \n")
print(dataframe1)
输出:
The Original Data Types of the Data frame are:
Attendance int64
Name object
Obtained Marks int64
dtype: object
The Modified Data Types of the Data frame are:
Attendance int32
Name object
Obtained Marks int64
dtype: object
该函数返回了转换的数据类型。我们使用 dtypes()
函数来显示 DataFrame 中各列的数据类型。
示例代码:DataFrame.astype()
方法改变 DataFrame 所有列的数据类型
我们将尝试改变给定 DataFrame 的数据类型。
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data Types of the Data frame are: \n")
print(dataframe.dtypes)
dataframe1 = dataframe.astype('object').dtypes
print("The Modified Data Types of the Data frame are: \n")
print(dataframe1)
输出:
The Original Data Types of the Data frame are:
Attendance int64
Name object
Obtained Marks int64
dtype: object
The Modified Data Types of the Data frame are:
Attendance object
Name object
Obtained Marks object
dtype: object
函数返回了修改后的 DataFrame,它已经将所有列的数据类型改为 object
。
示例代码:DataFrame.astype()
方法改变数据类型时有异常的情况
现在我们将数据类型 object
设置为 int32
。函数将忽略异常,因为我们将传递参数 errors= 'ignore'
。
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data Types of the Data frame are: \n")
print(dataframe.dtypes)
dataframe1 = dataframe.astype('int32', errors='ignore').dtypes
print("The Modified Data Types of the Data frame are: \n")
print(dataframe1)
输出:
The Original Data Types of the Data frame are:
Attendance int64
Name object
Obtained Marks int64
dtype: object
The Modified Data Types of the Data frame are:
Attendance int32
Name object
Obtained Marks int32
dtype: object
请注意,函数没有引发任何异常。它忽略了这个错误,因为我们将 object
转为 int32
。它只是没有改变 Name
列的数据类型。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串