【问题标题】:How convert column datatype int64 to categorical column datatype in python?如何在 python 中将列数据类型 int64 转换为分类列数据类型?
【发布时间】:2020-12-18 22:26:38
【问题描述】:

如何将 int 更改为分类

import pandas as pd
import numpy as np

data = pd.read_excel('data.xlsx',header=0)
data.info()

现在有一个 int64 列损坏。它显示了不同的损伤组。如何将此列转换为分类列? (背景是,有4个伤害组。1个不是真正的伤害,4个是完全伤害)。

我试过了:

data['damage']= data.damage.astype('categorical')

*data type 'categorical' not understood*

但它不起作用。例如,这适用于 ('float') 或其他任何东西。 列损坏如何转移到分类列?

【问题讨论】:

    标签: python pandas types type-conversion multiclass-classification


    【解决方案1】:

    根据pandas documentation 分类SeriesDataFrame 中的列可以通过多种方式创建。一种方法是将现有的 Series 或列转换为 category dtype。
    所以改变:

    data['damage']= data.damage.astype('categorical')
    

    收件人:

    data['damage'] = data.damage.astype('category')
    

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 2022-12-30
      • 2012-09-20
      • 1970-01-01
      • 2017-02-03
      • 2023-03-20
      • 2021-12-24
      • 2018-05-05
      • 2016-11-14
      相关资源
      最近更新 更多