Search code examples
pythondata-visualization

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


i want to find degree holders. The following code is causing AttributeError: 'float' object has no attribute 'find' and I do not how to fix it:


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

The full traceback for the error:

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


Solution

  • Does the image show all your data. Is it possible that somewhere in the data frame's Last_degree column contains a float?

    If yes, change this line:

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

    to

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