Pandas: change data type of Series to String

PythonPandasSeries

Python Problem Overview


I use Pandas 'ver 0.12.0' with Python 2.7 and have a dataframe as below:

df = pd.DataFrame({'id' : [123,512,'zhub1', 12354.3, 129, 753, 295, 610],
                    'colour': ['black', 'white','white','white',
                            'black', 'black', 'white', 'white'],
                    'shape': ['round', 'triangular', 'triangular','triangular','square',
                                        'triangular','round','triangular']
                    },  columns= ['id','colour', 'shape'])

The id Series consists of some integers and strings. Its dtype by default is object. I want to convert all contents of id to strings. I tried astype(str), which produces the output below.

df['id'].astype(str)
0    1
1    5
2    z
3    1
4    1
5    7
6    2
7    6

1) How can I convert all elements of id to String?

2) I will eventually use id for indexing for dataframes. Would having String indices in a dataframe slow things down, compared to having an integer index?

Python Solutions


Solution 1 - Python

You can convert all elements of id to str using apply

df.id.apply(str)

0        123
1        512
2      zhub1
3    12354.3
4        129
5        753
6        295
7        610

Edit by OP:

I think the issue was related to the Python version (2.7.), this worked:

df['id'].astype(basestring)
0        123
1        512
2      zhub1
3    12354.3
4        129
5        753
6        295
7        610
Name: id, dtype: object

Solution 2 - Python

A new answer to reflect the most current practices: as of now (v1.2.4), neither astype('str') nor astype(str) work.

As per the documentation, a Series can be converted to the string datatype in the following ways:

df['id'] = df['id'].astype("string")

df['id'] = pandas.Series(df['id'], dtype="string")

df['id'] = pandas.Series(df['id'], dtype=pandas.StringDtype)

Solution 3 - Python

You must assign it, like this:-

df['id']= df['id'].astype(str)

Solution 4 - Python

Personally none of the above worked for me. What did:

new_str = [str(x) for x in old_obj][0]

Solution 5 - Python

There are two possibilities:

Solution 6 - Python

You can use:

df.loc[:,'id'] = df.loc[:, 'id'].astype(str)

This is why they recommend this solution: Pandas doc

TD;LR

To reflect some of the answers:

df['id'] = df['id'].astype("string")

This will break on the given example because it will try to convert to StringArray which can not handle any number in the 'string'.

df['id']= df['id'].astype(str)

For me this solution throw some warning:

> SettingWithCopyWarning:  
> A value is trying to be set on a copy of a
> slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

Solution 7 - Python

For me it worked:

 df['id'].convert_dtypes()

see the documentation here:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.convert_dtypes.html

Solution 8 - Python

Your problem can easily be solved by converting it to the object first. After it is converted to object, just use "astype" to convert it to str.

obj = lambda x:x[1:]
df['id']=df['id'].apply(obj).astype('str')
    

Solution 9 - Python

use pandas string methods ie df['id'].str.cat()

Solution 10 - Python

for me .to_string() worked

df['id']=df['id'].to_string()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZhubarbView Question on Stackoverflow
Solution 1 - PythonAmitView Answer on Stackoverflow
Solution 2 - PythonrocksNwavesView Answer on Stackoverflow
Solution 3 - PythonRishil AntonyView Answer on Stackoverflow
Solution 4 - PythonmanesiozView Answer on Stackoverflow
Solution 5 - PythonRafael OrtegaView Answer on Stackoverflow
Solution 6 - Pythonuser3423349View Answer on Stackoverflow
Solution 7 - PythonGabrielView Answer on Stackoverflow
Solution 8 - Pythonshekhar chanderView Answer on Stackoverflow
Solution 9 - PythonJimmy Obonyo AborView Answer on Stackoverflow
Solution 10 - PythonYachika paulView Answer on Stackoverflow