Sorry, I've seen many question related with this error but even with all this information I can't solve it.
I have a dataframe df
with a column named int_rate
. The column's type is O
. It keeps the percentages, so each line is like: 10.95 %
I need to remove the %
sign to transform then the column in int
. I've tried with the following code:
df['int_rate']=df['int_rate'].apply(lambda x: x[:-1])
I get the following error:
TypeError: 'float' object is not subscriptable.
The first thing I don't understand is why float object if my column type is not float, and if so, how can I get rid of %
sign?
Probably your data contains some NaN
values, since NaN
are considered as float
type in Pandas.
You can do a df.info()
and look into the Non-Null Count
to check whether the count for column int_rate
corresponds to the existence of any NaN
value.
If there is any NaN
value, you can bypass the error by applying your code only to those non NaN
entries, as follows:
df['int_rate'] = df.loc[df['int_rate'].notna(), 'int_rate'].apply(lambda x: x[:-1])
Here, we filtered to process only those rows with the boolean mask: df['int_rate'].notna()
to exclude those NaN
entries.