John Constantine
John Constantine

Reputation: 1092

Pandas change column to string

I have a dataframe and when I do df.dtypes in shows the column is an object

I do df["column_name"] = df["column_name"].astype(str)

and again check df.dtypes it still shows the column type as object. Any idea bow to fix this.

When I do df['column_name'].to_string().dtype

I get an error AttributeError: 'unicode' object has no attribute 'dtype'

Upvotes: 0

Views: 990

Answers (2)

José De Souza
José De Souza

Reputation: 1

Series can be converted to the string datatype, give this a try:

df['column_name'] = pd.Series(df['column_name'], dtype="string")

Or give this a try

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

Upvotes: 0

Cmagelssen
Cmagelssen

Reputation: 670

In Pandas, there are only these datatypes: boolean, integer, float, datetime and object. Objects are almost always strings but can be any Python object. This is the reason why df.dtypes shows an object.

Upvotes: 1

Related Questions