You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中对多列应用类型转换且保留Id列?

问题:保留Id列的同时将其他整数列转为Categorical类型

我有一个包含Id列和多个整数列的DataFrame,希望将这些整数列转换为categorical类型,但保持Id列不变。目前的方法会丢弃Id列,请问如何实现该转换且不丢弃Id列?

当前代码:

df= df.loc[:, df.columns != 'Id'].apply(lambda x: x.astype('category'))

示例DataFrame:

{'Id': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}, 'Foundation': {0: 2, 1: 1, 2: 2, 3: 0, 4: 2}, 'GarageFinish': {0: 1, 1: 1, 2: 1, 3: 2, 4: 1}, 'LandSlope': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 'LotConfig': {0: 4, 1: 2, 2: 4, 3: 0, 4: 2}, 'GarageQual': {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}, 'GarageCond': {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}, 'LandContour': {0: 3, 1: 3, 2: 3, 3: 3, 4: 3}, 'Utilities': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 'GarageType': {0: 1, 1: 1, 2: 1, 3: 5, 4: 1}, 'LotShape': {0: 3, 1: 3, 2: 0, 3: 0, 4: 0}, 'Alley': {0: 2, 1: 2, 2: 2, 3: 2, 4: 2}, 'Street': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 'PoolQC': {0: 3, 1: 3, 2: 3, 3: 3, 4: 3}, 'Fence': {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}, 'MiscFeature': {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}, 'MSZoning': {0: 3, 1: 3, 2: 3, 3: 3, 4: 3}, 'SaleType': {0: 8, 1: 8, 2: 8, 3: 8, 4: 8}, 'PavedDrive': {0: 2, 1: 2, 2: 2, 3: 2, 4: 2}, 'FireplaceQu': {0: 5, 1: 4, 2: 4, 3: 2, 4: 4}, 'Condition1': {0: 2, 1: 1, 2: 2, 3: 2, 4: 2}, 'Functional': {0: 6, 1: 6, 2: 6, 3: 6, 4: 6}, 'BsmtQual': {0: 2, 1: 2, 2: 2, 3: 3, 4: 2}, 'BsmtCond': {0: 3, 1: 3, 2: 3, 3: 1, 4: 3}, 'BsmtExposure': {0: 3, 1: 1, 2: 2, 3: 3, 4: 0}, 'BsmtFinType1': {0: 2, 1: 0, 2: 2, 3: 0, 4: 2}, 'ExterQual': {0: 2, 1: 3, 2: 2, 3: 3, 4: 2}, 'BsmtFinType2': {0: 5, 1: 5, 2: 5, 3: 5, 4: 5}, 'MasVnrType': {0: 1, 1: 2, 2: 1, 3: 2, 4: 1}, 'Exterior2nd': {0: 13, 1: 8, 2: 13, 3: 15, 4: 13}, 'Heating': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 'Neighborhood': {0: 5, 1: 24, 2: 5, 3: 6, 4: 15}, 'SaleCondition': {0: 4, 1: 4, 2: 4, 3: 0, 4: 4}, 'Electrical': {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}, 'Exterior1st': {0: 12, 1: 8, 2: 12, 3: 13, 4: 12}, 'RoofMatl': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 'RoofStyle': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 'HouseStyle': {0: 5, 1: 2, 2: 5, 3: 5, 4: 5}, 'BldgType': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, 'Condition2': {0: 2, 1: 2, 2: 2, 3: 2, 4: 2}, 'KitchenQual': {0: 2, 1: 3, 2: 2, 3: 2, 4: 2}, 'ExterCond': {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}, 'CentralAir': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 'HeatingQC': {0: 0, 1: 0, 2: 0, 3: 2, 4: 0}}

解决方案

方法1:批量修改目标列类型(推荐)

直接定位除Id外的所有列,批量转换类型,原DataFrame结构会完整保留:

# 获取非Id列的列表
non_id_cols = df.columns[df.columns != 'Id']
# 转换这些列为categorical类型
df[non_id_cols] = df[non_id_cols].astype('category')

方法2:合并处理后的列与Id列

如果需要先单独处理非Id列,可以将处理结果和原Id列拼接:

import pandas as pd

# 处理非Id列
categorical_df = df.loc[:, df.columns != 'Id'].astype('category')
# 合并Id列和处理后的列
df = pd.concat([df['Id'], categorical_df], axis=1)

方法3:使用apply条件转换

通过apply遍历每一列,仅对非Id列进行类型转换:

df = df.apply(lambda col: col.astype('category') if col.name != 'Id' else col)

验证转换结果:执行df.dtypes可以查看各列类型,确认Id列保持原有整数类型,其他列均转为category类型。

内容的提问来源于stack exchange,提问作者Katsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:20:24