Python – 获取列的数据类型
pythonserver side programmingprogramming更新于 2025/11/9 13:32:17
要获取列的数据类型,请使用 info() 方法。让我们首先导入所需的库 −
import pandas as pd
创建一个 DataFrame,其中包含 2 列具有不同的数据类型 −
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
}
)
获取数据类型的完整信息 −
dataFrame.info()
示例
以下是完整代码 −
import pandas as pd
# 创建 DataFrame
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
}
)
print"DataFrame ...\n",dataFrame
print"\nInfo 和 dataframe 中列的数据类型:\n"
# 获取数据类型信息
print(dataFrame.info())
输出
这将产生以下输出 −
DataFrame ... Roll Number Student 0 5 Jack 1 10 Robin 2 3 Ted 3 8 Marc 4 2 Scarlett 5 9 Kat 6 6 John Info and the datatypes of the columns in the dataframe: <class 'pandas.core.frame.DataFrame'> RangeIndex: 7 entries, 0 to 6 Data columns (total 2 columns): Roll Number 7 non-null int64 Student 7 non-null object dtypes: int64(1), object(1) memory usage: 184.0+ bytes None
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

