GabyLP GabyLP - 1 year ago 57
Python Question

python apply function to df pandas - atribute error

I have a data frame "b" with numbers stored as text like '12.5%'.
One column is:

1 NaN
2 NaN
3 1.2%
4 0.6%
5 NaN
6 1.4%
7 0.1%
8 NaN
9 5.1%
10 2.5%
11 89.1%
12 NaN
Name: Idaho, dtype: object

I've wrote a function to apply to each column:

def sinPorc(tbl):
return float(tbl.replace('%', ''))

but when I try to apply it I get:


AttributeError: 'float' object has no attribute 'replace'

I 've also created an example table, and apply the function but in this case worked:

ejemplo=pd.DataFrame({'A':['1.3%', 'NaN'],
'B':['1.3%', '0.7%']})


and I got the expected result:

Name: A, dtype: float64

I don't know why I cannot apply the function to the original table. What might be the problem and what should I do to solve it?


Answer Source

Your original method didn't work because NaN was not a string, but the float value np.NaN

Try this...

np.NaN.replace('%', '')

and you will get the same error.

AttributeError: 'float' object has no attribute 'replace'

You could change sinPorc to

def sinPorc(tbl):
    if tbl is np.NaN:
        return tbl
        return float(tbl.replace('%', ''))

Which will preserver your NaN values which are useful for other Pandas functionality, or you could force the value to a string like Nickil mentioned in the comments.

float(str(tbl).replace('%', ''))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download