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

问题描述 投票:0回答:2

我想找到学位持有者。 以下代码导致 AttributeError: 'float' object has no attribute 'find' 而且我不知道如何修复它:


edu = Edu_data

# Function to identify degree
def degree(x):
    #if x.find('Bachelor') != -1 or x.find("Bachelor's") != -1 or x.find('BS') != -1 or x.find('bs') != -1 or x.find('Bs') != -1 or x.find('Bachelors') != -1 or x.find('Undergraduate') != -1 or x.find('graduated')!= -1 or x.find('BSE')!= -1 or x.find('Enginee') != -1 or x.find('BCS') != -1:
    if x.find('Bachelor') != -1 or x.find("Bachelor's") != -1 or x.find('BS') != -1 or x.find('bs') != -1:
        return(1)
    if x.find('Master') != -1 or x.find("Master's") != -1 or x.find('M.S') != -1 or x.find('MS') != -1 or x.find('MPhil') != -1 or x.find('MBA') != -1 or x.find('MicroMasters') != -1 or x.find('MSc') != -1 or x.find('MSCS') !=-1 or x.find('MSDS')!=-1:
        return(2)
    if x.find('PhD') != -1 or x.find('P.hd') != -1 or x.find('Ph.D') != -1 or x.find('ph.d') != -1:
        return(3)
    else:
        return(0)

    
# Create degree column
edu['deg'] = list(map(degree, edu['Last_degree']))

edu

错误的完整回溯:

AttributeError                            Traceback (most recent call last)
<ipython-input-39-9a79283a9b17> in <module>
     16 
     17 # Create degree column
---> 18 edu['deg'] = list(map(degree, edu['Last_degree']))
     19 
     20 edu

<ipython-input-39-9a79283a9b17> in degree(x)
      5 # Function to identify degree
      6 def degree(x):
----> 7     if x.find('Bachelor') != -1 or x.find("Bachelor's") != -1 or x.find('BS') != -1 or x.find('bs') != -1 or x.find('Bs') != -1 or x.find('Bachelors') != -1 or x.find('Undergraduate') != -1 or x.find('graduated')!= -1 or x.find('BSE')!= -1 or x.find('Enginee') != -1 or x.find('BCS') != -1:
      8         return(1)
      9     if x.find('Master') != -1 or x.find("Master's") != -1 or x.find('M.S') != -1 or x.find('MS') != -1 or x.find('MPhil') != -1 or x.find('MBA') != -1 or x.find('MicroMasters') != -1 or x.find('MSc') != -1 or x.find('MSCS') !=-1 or x.find('MSDS')!=-1:

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

image of dataset

python data-visualization
2个回答
2
投票

图像是否显示了您的所有数据。数据框的

Last_degree
列中的某处是否有可能包含
float

如果是,请更改此行:

edu['deg'] = list(map(degree, edu['Last_degree']))

edu['deg'] = list(map(degree, edu['Last_degree'].astype(str)))

0
投票

由于 Nan 值,通过用某些东西替换来删除它们

© www.soinside.com 2019 - 2024. All rights reserved.