Python - 选择具有特定数据类型的列
要选择具有特定数据类型的列,请使用select_dtypes()方法和include参数。首先,创建一个包含2列的DataFrame-
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
}
)现在,选择具有各自特定数据类型的2列-
column1 = dataFrame.select_dtypes(include=['object']).columns column2 = dataFrame.select_dtypes(include=['int64']).columns
示例
以下是代码-
import pandas as pd
#CreateDataFrame
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 of the entire dataframe:\n"
#getthedescription
print(dataFrame.info())
#selectcolumnswithspecificdatatype
column1 = dataFrame.select_dtypes(include=['object']).columns
column2 = dataFrame.select_dtypes(include=['int64']).columns
print"Column 1 with object type = ",column1
print"Column 2 with int64 type = ",column2输出结果这将产生以下输出-
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 of the entire 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 Column 1 with object type = Index([u'Student'], dtype='object') Column 2 with int64 type = Index([u'Roll Number'], dtype='object')
热门推荐
10 顾客简短祝福语大全集
11 节日祝福语幽默简短的
12 圣诞祝福语简短小学
13 新娘上头祝福语大全简短
14 送灯祝福语简短独特
15 很暧昧的简短祝福语
16 近代生日祝福语简短精辟
17 接春的祝福语简短
18 别人过大寿祝福语简短