将所有对象列dtypes转换为分类

时间:2014-09-30 04:05:06

标签: python pandas

我有一个导入Pandas的电子表格,它都是字符串。我想将所有列转换为类别。 我用单个类别测试了这个并没有问题,但是当我尝试迭代地进行测试时,我似乎无法改变dtypes。

模拟数据:

a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0']]
df = pd.DataFrame(a, columns=['one', 'two', 'three'])

# works
pd.Categorical(a['one'])
new_one = pd.Categorical(df['one'])

#doesn't work
df['one'] = pd.Categorical(df['one'])

# also doesn't work if I try to create a new df 
# and populate it with the iterated results

for i in list(df.columns):
    df[i] = pd.Categorical(df[i])

1 个答案:

答案 0 :(得分:0)

如果可以DataFrame.astype使用pandas 0.23.0,请阅读更多here

df = df.astype('category')
print (df.dtypes)
one      category
two      category
three    category
dtype: object