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`
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
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`
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