Here's what I say:
df["rating"] = df["rating"].astype(str, errors='ignore')
df.dtypes
Here's what it prints:
name object
platform object
year_of_release int64
genre object
na_sales float64
eu_sales float64
jp_sales float64
other_sales float64
critic_score float64
user_score float64
rating object
total_sales float64
reviewed bool
dtype: object
As you can see, rating is still an object and not a string. Why is it so?
CodePudding user response:
As of Pandas 1.0, the extension types, StringDtype was introduced. Per Work with text data docs:
There are two ways to store text data in pandas:
object-dtype NumPy array.StringDtypeextension type.We recommend using
StringDtypeto store text data.Prior to pandas 1.0, object
dtypewas the only option.
Currently, with astype(str), you are rendering an object type which can contain any mix of types including numbers and strings. However, since Pandas 1.0 , you can now use a dedicated string type:
df["rating"] = df["rating"].astype("string")
CodePudding user response:
Try using this command
df["rating"]=df["rating"].apply(obj).astype('str')
