Home > front end >  How to deal this error? TypeError: object of type 'float' has no len()
How to deal this error? TypeError: object of type 'float' has no len()

Time:05-05

drugbank = pd.read_excel('E:/Python工程/数据挖掘实例/test/drugbank/drugbank_extract.xlsx')

print(drugbank)
#从一系列逗号分隔的字符串中返回列表
def chainer(s):
    return list(chain.from_iterable(s.str.split('|')))

#计算分裂的长度
lens = drugbank['synonyms'].str.split('|').map(len)

#创建新的数据框,根据需要重复或链接
res = pd.DataFrame({'drugbankid': np.repeat(drugbank['drugbankid'], lens),
                    'commonname': np.repeat(drugbank['commonname'], lens),
                    'synonyms': chainer(drugbank['synonyms'])})
print(res)

When I run the above code, I always get the following error, how should I solve it?

TypeError: object of type 'float' has no len()

CodePudding user response:

lens = len(drugbank['synonyms'].str.split('|'))

CodePudding user response:

What usually causes this problem?

Float objects do not have length properties - so the len() function won't be able to find a property it does not have. If you want to find the length of the float, convert it to a string first, using the str() function. Encase the str() function in a len() function, and bam - you get the length of the float.

>>>my_float = 1.3424561
>>>len(str(my_float))
9

The solution and problem in this case

len() is a function, not a parameter. You need to define len as:

lens = len(drugbank['synonyms'].str.split('|'))

P.S. It is bad practice to name variables names similar to built in functions and methods, due to possible confusion caused.

What else may cause this problem?

In python, map returns an iterator. Therefore, you need to convert your value into a list using the list() function - which you did perfectly in the chainer function, but less perfectly when defining lens. You're lens definition should be:

lens = list(drugbank['synonyms'].str.split('|').map(len))

Also, len should not go inside map - hence

What else could cause this problem?

However, in your specific case, as you are not dealing with any values that are explicitly defined as a float (at least from our view), I would assume the float is coming from the document (database). In your case, just wrap all definitions in the str() function - This way you will be assured to get the one that is the problem.

More "correct" solution

Rather than wrapping everything in str(), you can do some debugging. With debugging tools and break points, you can tell exactly when this error occurs, and, in turn, only have to wrap that section in str(). Your error message may also you the line this is occurring on - no debugging tools needed.

Why work with strings?

The nice thing about strings is they can represent other data types, so you can work with multiple data types, and then convert it back once you're done.

  • Related