Skip to content
Advertisement

Applying function to Column AttributeError: ‘int’ object has no attribute

I have a pandas data frame that consists of special/vanity numbers.

numbers = [539249751,530246444,539246655,539209759,538849098]
  
# Create the pandas DataFrame with column name is provided explicitly
vanity_class= pd.DataFrame(numbers, columns=['MNM_MOBILE_NUMBER'])

I would like to add a column to classify each number based on its pattern using regex.

I have written a function that iterates through the column MNM_MOBILE_NUMBER. Identifies the pattern of each number using regex. Then, creates a new column MNC_New_Class with the relevant classification.

def vanity_def(vanity_class):
    if vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'^5(d)1{7}') | 
            vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'^5(?!(d)1)d(d)2{6}$') | 
            vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'.{2}(?!(d)1)d(d)2{5}$') | 
            vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'^d*(d)(d)(?:12){3}d*$') | 
            vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'^5((d)2{3})((d)4{3})$') | 
            vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'.{3}(1234567$)'):
        vanity_class['MNC_New_Class'] = 'Diamond'
    elif vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'.{3}(?!(d)1)d(d)2{4}$') | 
             vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'^(?!(d)1)d((d)3{6})(?!3)d$') | 
             vanity_class.MNM_MOBILE_NUMBER.astype(str).str.match(r'd(d)1(d)2(d)3(d)4'):     
        vanity_class['MNC_New_Class'] = 'Gold'
    else:
        vanity_class['MNC_New_Class'] = 'Non Classified'

Then, I wrote this line of code to apply the function to the column.

vanity_class['MNC_New_Class']  = vanity_class['MNM_MOBILE_NUMBER'].apply(vanity_def)

However, I keep getting this error

AttributeError: ‘int’ object has no attribute ‘MNM_MOBILE_NUMBER’

Any advice on how to avoid this error?

Thank you

Advertisement

Answer

When you pass a function to Pandas’ apply(), it receives the value of the selected column, not the data frame itself. So you should rewrite your code accordingly:

def vanity_def(mnm_mobile_number): # parameter is an int
    # return the new value, do the assignment outside of this function

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement