[英]How convert column datatype int64 to categorical column datatype in python?
how can i change int to categorical我如何将 int 更改为分类
import pandas as pd
import numpy as np
data = pd.read_excel('data.xlsx',header=0)
data.info()
there is now a column damage which is int64.现在有一个 int64 列损坏。 It shows different damage-groups.它显示了不同的伤害组。 How can this column be convert to a categorical column?如何将此列转换为分类列? (background is, there are 4 damage groups. 1 not really damage, 4 is totally damage). (背景是,有 4 个伤害组。1 不是真正的伤害,4 是完全伤害)。
I tried:我试过:
data['damage']= data.damage.astype('categorical')
*data type 'categorical' not understood*
but it doesn´t work.但它不起作用。 This works for example with ('float') or anything else.例如,这适用于 ('float') 或其他任何东西。 How can the column damage transfer to a categorical column?列损坏如何转移到分类列?
According to pandas documentation categorical Series
or columns in a DataFrame
can be created by several ways.根据DataFrame
文档, DataFrame
分类Series
或列可以通过多种方式创建。 One way is converting an existing Series
or column to a category
dtype.一种方法是将现有的Series
或列转换为category
dtype。
So change:所以改变:
data['damage']= data.damage.astype('categorical')
To:到:
data['damage'] = data.damage.astype('category')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.