无法在python数据框中将列类型从对象转换为str [英] can not convert column type from object to str in python dataframe

查看:62
本文介绍了无法在python数据框中将列类型从对象转换为str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了一个csv文件,然后将其读取到python数据帧中,现在所有4列都具有对象类型,我想将它们转换为str类型,

i have downloaded a csv file, and then read it to python dataframe, now all 4 columns all have object type, i want to convert them to str type,

现在dtype的结果如下:

and now the result of dtypes is as follows:

Name                      object
Position Title            object
Department                object
Employee Annual Salary    object
dtype: object

我尝试使用以下方法更改类型:

i try to change the type using the following methods:

path['Employee Annual Salary'] = path['Employee Annual Salary'].astype(str)

,但dtypes仍返回类型对象,
,我在读取csv时也尝试提供列类型,

but dtypes still return type object, and i also try to provide the column type when reading csv,

path = pd.read_csv("C:\\Users\\IBM_ADMIN\\Desktop\\ml-1m\\city-of-chicago-salaries.csv",dtype={'Employee Annual Salary':str})

path = pd.read_csv("C:\\Users\\IBM_ADMIN\\Desktop\\ml-1m\\city-of-chicago-salaries.csv",dtype=str)

但仍然无法正常工作,
想知道如何将列类型从对象更改为str,

but still do not work, want to know how to change column type from object to str,

推荐答案

对于字符串,列类型将始终为对象。无需进行任何转换;

For strings, the column type will always be 'object.' There is no need for you convert anything; it is already doing what you require.

类型来自numpy,它具有一组数字数据类型。

The types come from numpy, which has a set of numeric data types. Anything else is an object.

您可能想阅读 http://nbviewer.jupyter.org/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/02.01-Understanding-Data-Types.ipynb 以获得更完整的解释。

You might want to read http://nbviewer.jupyter.org/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/02.01-Understanding-Data-Types.ipynb for a fuller explanation.

这篇关于无法在python数据框中将列类型从对象转换为str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆