Fun Project: Phone Numbers Validation Tool using NumVerify API
Author: LI LIU (1st yr master’s student at UChicago)
10/26/18: basic framework
10/27/18: add multiple conditions for verifying valid numbers
import pandas as pd
#Read in file
Num = pd.read_excel('PhoneSurvey.xlsx',
names=['ID','Phone','Response','Party','Age'])
Example: Random-genrated Numbers in Philadelphia (Code 267)
Num["Code"] = 267
Num["PN"] = Num["Code"].astype(str) + Num["Phone"].astype(str)
Num.head()
ID | Phone | Response | Party | Age | Code | PN | |
---|---|---|---|---|---|---|---|
0 | 1 | 4310454 | NaN | NaN | NaN | 267 | 2674310454 |
1 | 2 | 6920338 | NaN | NaN | NaN | 267 | 2676920338 |
2 | 3 | 6918070 | NaN | NaN | NaN | 267 | 2676918070 |
3 | 4 | 3605305 | NaN | NaN | NaN | 267 | 2673605305 |
4 | 5 | 8892229 | NaN | NaN | NaN | 267 | 2678892229 |
Create a free account to get access key
One account is good for to 250 API requests.
Go to https://numverify.com/documentation!
Define the fucntion
import urllib.request, json
list=[]
#Replace with access key
key = "###"
def validtest(number):
'''
Return the indicator list of whether number is valid or not.
'''
link = "http://apilayer.net/api/validate?access_key="+key+"&number="+\
number + "&country_code=US&format=1"
with urllib.request.urlopen(link) as url:
data = json.loads(url.read().decode())
#Specify conditions for valid numbers
if data["valid"] == True and data['carrier'] !='' and data['location'] !='' and data['line_type'] !='':
list.append(1)
else:
list.append(0)
return data
Run the function
for num in Num["PN"]:
validtest(num)
res = pd.Series(list)
Num["Label"] = res.values
#Counts of valid numbers
sum(list)
60
#Test single number
#validtest("my number")
Num.head()
ID | Phone | Response | Party | Age | Code | PN | Label | |
---|---|---|---|---|---|---|---|---|
0 | 1 | 4310454 | NaN | NaN | NaN | 267 | 2674310454 | 1 |
1 | 2 | 6920338 | NaN | NaN | NaN | 267 | 2676920338 | 0 |
2 | 3 | 6918070 | NaN | NaN | NaN | 267 | 2676918070 | 0 |
3 | 4 | 3605305 | NaN | NaN | NaN | 267 | 2673605305 | 0 |
4 | 5 | 8892229 | NaN | NaN | NaN | 267 | 2678892229 | 1 |
Save the output
Num.to_excel('data.xlsx')
Leave a Comment