Converting a column within pandas dataframe from int to string
In [16]: df = DataFrame(np.arange(10).reshape(5,2),columns=list('AB'))In [17]: dfOut[17]: A B0 0 11 2 32 4 53 6 74 8 9In [18]: df.dtypesOut[18]: A int64B int64dtype: object
Convert a series
In [19]: df['A'].apply(str)Out[19]: 0 01 22 43 64 8Name: A, dtype: objectIn [20]: df['A'].apply(str)[0]Out[20]: '0'
Don't forget to assign the result back:
df['A'] = df['A'].apply(str)
Convert the whole frame
In [21]: df.applymap(str)Out[21]: A B0 0 11 2 32 4 53 6 74 8 9In [22]: df.applymap(str).iloc[0,0]Out[22]: '0'
df = df.applymap(str)
Warning: Both solutions given ( astype() and apply() ) do not preserve NULL values in either the nan or the None form.
import pandas as pdimport numpy as npdf = pd.DataFrame([None,'string',np.nan,42], index=[0,1,2,3], columns=['A'])df1 = df['A'].astype(str)df2 = df['A'].apply(str)print df.isnull()print df1.isnull()print df2.isnull()
I believe this is fixed by the implementation of to_string()