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,
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)
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})
or
path = pd.read_csv("C:\\Users\\IBM_ADMIN\\Desktop\\ml-1m\\city-of-chicago-salaries.csv",dtype=str)
but still do not work, want to know how to change column type from object to str,
Actually you can set the type of a column to string
. Use .astype('string')
rather than .astype(str)
.
Sample Data Set
df = pd.DataFrame(data={'name': ['Bla',None,'Peter']})
The column name is by default a object
.
Single Column Solution
df.name = df.name.astype('string')
It's important to write .astype('string')
rather than .astype(str)
which didn't work for me. It will stay as object
as you do so.
Multi-Column Solution
df = df.astype(dtype={'name': 'string'})
Allows to change multiple fields at once.
For strings, the column type will always be 'object.' There is no need for you convert anything; it is already doing what you require.
The types come from numpy, which has a set of numeric data types. Anything else is an object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With