Can not coerce an object column to string type in pandas

Question:

I have a column that should have been "born" as type str. But given it was not -but rather as object, how in the world can I get it to be of type str ? i’ve tried a few approaches, which had been documented as supposed to work especially here How to convert column with dtype as object to string in Pandas Dataframe

Given a dataframe dfm and a list of feature values feats

            # this should work as str's but does not
            dfm.insert(colx,'top20pct_features', [str(x) for x in feats])

            # let's try another way.. but also does not work
            dfm.top20pct_features = dfm.top20pct_features.astype(str)

            # another way.. same story..
            dfm.top20pct_features = dfm.top20pct_features.str
            print(dfm.info())  # reports the column as `object`
Asked By: WestCoastProjects

||

Answers:

You can use convert_dtypes to benefit from the relatively recent string dtype:

df['top20pct_features'] = df['top20pct_features'].convert_dtypes()

Example:

df = pd.DataFrame({'top20pct_features': ['A', 'B', 'C']})

df.dtypes
top20pct_features    object
dtype: object

df['top20pct_features'] = df['top20pct_features'].convert_dtypes()

df.dtypes
top20pct_features    string
dtype: object
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.