pandas 如何筛选data_type= object的列

4zcjmb1e  于 2023-01-07  发布在  其他
关注(0)|答案(2)|浏览(178)
encoder=LabelEncoder()
categorical_features=df.columns.tolist()
for col in categorical_features:
    df[col]=encoder.fit_transform(df[col])
df.head(20)

**我希望categorical_features采用数据类型为object的列

fhity93d

fhity93d1#

试试这个

for col in categorical_features:
    if df.dtypes[col] == "object":
        print('object type')
    else:
        print(f'something else {df.dtypes[col]}')

你可以修改你的代码

categorical_features=df.columns.tolist()
for col in categorical_features:
    if df.dtypes[col] == "object"
       df[col]=encoder.fit_transform(df[col])
67up9zun

67up9zun2#

要选择Pandas中的所有对象类型列,可以用途:

categorical_features = df.select_dtypes(include=['object'])

select_dtypes()在Pandasv 0.14.1中发布

相关问题