Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not coerce an object column to string type in pandas

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`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading