Changing cloumn type from float to object/string in pandas

How do I change column type from float to object/string.

1 Like

will you elaborate more ? cause you are missing how you want to convert it.
suppose, if you want to just convert 23.45 into string “23.45” then you can do as follows:

assumptions :
let ds be the dataframe with a column name “cname” of float type which you want to convert into string.

 ds["cname"] = [str(item) for item in ds["cname"]] 

let me know if it works.

my columns type is float i.e with values 23.0, 45.0, 65.0 and so on…i want it as object/string type as 23, 45, 65 and so on…

1 Like

make a slight change in above code as :

ds["cname"] = [str(int(item)) for item in ds["cname"]]

ok i wrote it like this:

fatal_encounters_df[“Unique ID”] = [str(int(item)) for item in fatal_encounters_df[“Unique ID”]]

Getting below error:

ValueError Traceback (most recent call last)
in
----> 1 fatal_encounters_df[“Unique ID”] = [str(int(item)) for item in fatal_encounters_df[“Unique ID”]]

in (.0)
----> 1 fatal_encounters_df[“Unique ID”] = [str(int(item)) for item in fatal_encounters_df[“Unique ID”]]

ValueError: cannot convert float NaN to integer

i want to change the Unique id , dtype from float to object:

Column Non-Null Count Dtype


0 Unique ID 28621 non-null float64
1 Subjects age 27608 non-null object
2 Subjects gender 28521 non-null object

It seems like some of the items of the column are NaN type, so neither it cannot be converted into integer not string. For this, I would recommend you to do some data preprocessing that involves conversion of NaN into 0.0 or removing of that entire row depends upon the nature of the data set.

checked the raw file zero nan values unique id starts with 1 and ends with 28621… even the .info shows the same…

Column Non-Null Count Dtype


0 Unique ID 28621 non-null float64
1 Subjects age 27608 non-null object
2 Subjects gender 28521 non-null object
3 Subjects race 28621 non-null object
4 Injury date 28622 non-null datetime64[ns]

You can avoid this with a mask method. Note first that in python NaN is defined as the number which is not equal to itself:

float(‘nan’) == float(‘nan’)
False

The ValueError: cannot convert float NaN to integer raised because of Pandas doesn’t have the ability to store NaN values for integers. From Pandas v0.24, introduces Nullable Integer Data Types which allows integers to coexist with NaNs. This does allow integer NaNs . This is the pandas integer, instead of the numpy integer. So, use Nullable Integer Data Types (e.g. Int64).

df['x'].astype('Int64')

NB: You have to go through numpy float first and then to nullable Int32, for some reason.