0

I need to verify phone numbers as valid and invalid from my data. I am using phonenumbers library in python

I have created a for loop which works but it is too slow so I was trying to use same for loop inside apply function but getting index error


    for i in range(len(df)):

        num = df.loc[i,'Primary Phone #']
        region = df.loc[i,'Override Address Country']
        try:
            output = phonenumbers.parse(num, region=region)
        except phonenumbers.NumberParseException:
            df.loc[i,'validation'] = False
        else:
            df.loc[i,'validation'] = phonenumbers.is_valid_number(output)

temp_data.apply(number_validation, axis = 0/1)

IndexingError: ('Too many indexers', 'occurred at index Work Order Code')

1 Answer 1

1

I would probably use apply, like you tried, but make sure to supply the arguments:

def verify_number(x):
    return phonenumbers.parse(
        x['Primary Phone #'],
        region=x['Override Address Country']
    )

df.apply(verify_number, axis=1)

If that still is too slow, you could consider enhancing performance with Cython, Numba and pandas.eval(), as outlined in the pandas user guide.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.