Pandas: change data type of Series to String Pandas: change data type of Series to String python python

Pandas: change data type of Series to String


You can convert all elements of id to str using apply

df.id.apply(str)0        1231        5122      zhub13    12354.34        1295        7536        2957        610

Edit by OP:

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

df['id'].astype(basestring)0        1231        5122      zhub13    12354.34        1295        7536        2957        610Name: id, dtype: object


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)


You must assign it, like this:-

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