Reputation: 59
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')
Upvotes: 0
Views: 27
Reputation: 7164
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.
Upvotes: 1