获取Pandas DataFrame的列的数据类型
让我们看看如何在Pandas DataFrame中获得列的数据类型。为了获得数据类型,我们将使用dtype()和type()函数。
例1 :
# importing the module
import pandas as pd
# creating a DataFrame
dictionary = {'Names':['Simon', 'Josh', 'Amen', 'Habby',
'Jonathan', 'Nick', 'Jake'],
'Countries':['AUSTRIA', 'BELGIUM', 'BRAZIL',
'JAPAN', 'FRANCE', 'INDIA', 'GERMANY'],
'Boolean':[True, False, False, True,
True, False, True],
'HouseNo':[231, 453, 723, 924, 784, 561, 403],
'Location':[12.34, 45.67, 03.45, 17.23,
83.12, 90.45, 84.34]}
table = pd.DataFrame(dictionary, columns = ['Names', 'Countries',
'Boolean', 'HouseNo', 'Location'])
print("Data Types of The Columns in Data Frame")
display(table.dtypes)
print("Data types on accessing a single column of the Data Frame ")
print("Type of Names Column : ", type(table.iloc[:, 0]))
print("Type of HouseNo Column : ", type(table.iloc[:, 3]), "\n")
print("Data types of individual elements of a particular columns Data Frame ")
print("Type of Names Column Element : ", type(table.iloc[:, 0][1]))
print("Type of Boolean Column Element : ", type(table.iloc[:, 2][2]))
print("Type of HouseNo Column Element : ", type(table.iloc[:, 3][4]))
print("Type of Location Column Element : ", type(table.iloc[:, 4][0]))
输出
从输出结果中我们可以观察到,在访问或获取从DataFrame中分离出来的单列时,其类型被转换为Pandas系列类型,而不考虑该系列中存在的数据类型。在访问pandas系列的单个元素时,我们得到的数据总是以numpy.datatype()的形式存储,要么是numpy.int64或numpy.float64或numpy.bool_,因此我们观察到,Pandas数据框架自动将数据类型转换为NumPy类格式。
例子2 :
# importing the module
import pandas as pd
# creating a DataFrame
data = {'Name' : ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age' : [27, 24, 22, 32],
'Address' : ['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification' : ['Msc', 'MA', 'MCA', 'Phd']}
table = pd.DataFrame(data)
print("Data Types of The Columns in Data Frame")
display(table.dtypes)