0

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`
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
  • `string` is obviously `object`, if need dtype `string` use `df['top20pct_features'] = df['top20pct_features'].astype('string')` – jezrael Sep 30 '22 at 06:50
  • Duplicate explain why get `object` for string column created by `.astype(str) ` – jezrael Sep 30 '22 at 06:51

1 Answers1

1

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
mozway
  • 194,879
  • 13
  • 39
  • 75