I have some columns ['a', 'b', 'c', etc.] (a and c are float64 while b is object)
I would like to convert all columns to string and preserve nans.
Tried using df[['a', 'b', 'c']] == df[['a', 'b', 'c']].astype(str) but that left blanks for the float64 columns.
Currently I am going through one by one with the following:
df['a'] = df['a'].apply(str) df['a'] = df['a'].replace('nan', np.nan) Is the best way to use .astype(str) and then replace '' with np.nan? Side question: is there a difference between .astype(str) and .apply(str)?
Sample Input: (dtypes: a=float64, b=object, c=float64)
a, b, c, etc. 23, 'a42', 142, etc. 51, '3', 12, etc. NaN, NaN, NaN, etc. 24, 'a1', NaN, etc. Desired output: (dtypes: a=object, b=object, c=object)
a, b, c, etc. '23', 'a42', '142', etc. '51', 'a3', '12', etc. NaN, NaN, NaN, etc. '24', 'a1', NaN, etc.