pythonpandaslibphonenumber

Validation phonenumbers column python pandas with phonenumbers library


I want to check multiple phone numbers from my dataframe with phonenumbers library https://pypi.org/project/phonenumbers/

I want to validate the phone numbers and eventually i want to know from which country the number is. So for example:

contact phoneNumber phoneCheck phoneCountry
1 31650868016 True Netherlands
2 447986123456 True United Kingdom
3 55677 False

I used this solution: https://stackoverflow.com/a/56782746 I made a Country column.

But I want to use phonenumbers.is_valid_number() function and eventually the geocoder.description_for_number() function.

df['phone_number_clean'] = df.apply(lambda x: 
                              phonenumbers.is_valid_number(phonenumbers.is_valid_number(str(x.phoneNumber), 
                                                 str(x.Country)), 
                              axis='columns'))

Error: AttributeError: 'Series' object has no attribute 'phoneNumber'


Solution

  • The specific issue causing this error is a misplaced parantheses after axis instead of before:

    df['phone_number_clean'] = df.apply(lambda x: phonenumbers.is_valid_number(phonenumbers.is_valid_number(str(x.phoneNumber), str(x.phoneCountry))), axis='columns')
    

    I think below is more what you're looking for though:

    df['phone_number_clean'] = df.apply(lambda x: phonenumbers.is_valid_number(phonenumbers.parse("+"+str(x.phoneNumber), None)), axis=1)
    

    I used None as a placeholder because you didn't have country code in your DataFrame.