AttributeError: 'float' object has no attribute 'lower' | Python

Post Reply
User avatar
Aisangam
Site Admin
Posts: 4
Joined: 24 Mar 2019
Location: Delhi, India
Contact:

AttributeError: 'float' object has no attribute 'lower' | Python

Post by Aisangam » 26 Mar 2019

I have come across this error many times while playing with pandas. I have been trying to transform all the rows of a data frame into lower case. While I was performing the task i.e is

Code: Select all

df['column']=df['column'].apply(lambda x:x.lower())
I got the below error:-

Code: Select all

AttributeError                            Traceback (most recent call last)
<ipython-input-54-1e4a3e86917e> in <module>()
----> 1 read_file['question']=read_file['question'].apply(lambda x:x.lower())

/home/gpu/.local/lib/python3.6/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   3589             else:
   3590                 values = self.astype(object).values
-> 3591                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3592 
   3593         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-54-1e4a3e86917e> in <lambda>(x)
----> 1 read_file['question']=read_file['question'].apply(lambda x:x.lower())

AttributeError: 'float' object has no attribute 'lower'
One of the solution I got to solve this error is as below

Code: Select all

df['column']=df['column'].astype(str).apply(lambda x:x.lower())
This will convert each row of the dataframe into the string so that lower function can be applied. This is one of the solution i got. I hope there are may be some more better solutions than this.

Post Reply