题目要求:随机生成10个电话号码,在号码归属表中查询其对应归属地信息
号码归属表部分显示:
号码段 | 归属地省份 | 归属地城市 | 卡类型 | 区号 | 邮政编码 |
1300000 | 山东 | 济南市 | 山东联通130卡 | 531 | 250000 |
1300001 | 江苏 | 常州市 | 江苏联通130卡 | 519 | 213000 |
1300002 | 安徽 | 巢湖市 | 安徽联通130卡 | 565 | 238000 |
1300003 | 四川 | 宜宾 | 四川联通130卡 | 831 | 644000 |
期望得到结果如下:
号码 | 归属地省份 | 归属地城市 | 卡类型 | 区号 | 邮政编码 | |
0 | 15269532161 | 山东 | 烟台市 | 山东移动GSM卡 | 535 | 264000 |
1 | 13914862190 | 江苏 | 徐州市 | 江苏移动GSM卡 | 516 | 221000 |
2 | 13185737454 | 浙江 | 嘉兴市 | 浙江联通GSM卡 | 573 | 314000 |
3 | 15290688450 | 河南 | 周口市 | 河南移动GSM卡 | 394 | 466000 |
4 | 13207301620 | 湖南 | 岳阳市 | 湖南联通GSM卡 | 730 | 414000 |
5 | 18843924713 | 吉林 | 白山市 | 吉林移动188卡 | 439 | 134300 |
6 | 13359099817 | 安徽 | 黄山市 | 安徽电信CDMA卡 | 559 | 242700 |
7 | 18931736956 | 河北 | 沧州市 | 河北电信CDMA卡 | 317 | 61000 |
8 | 18620973590 | 广东 | 广州市 | 广东联通GSM卡 | 20 | 510000 |
9 | 15682836801 | 甘肃 | 兰州市 | 甘肃联通GSM卡 | 931 | 730000 |
思路1:循环
#导入包
import pandas as pd
import random
#读入号码归属.csv
df=pd.read_csv(r'C:\Users\Desktop\号码归属.csv',engine='python')
def createPhone():
phones=[]
for k in range(10):
prelist=["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
"147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
"186", "187", "188", "189"]
tel=random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))
phones.append(tel)
return phones
def GetphoneGuishudi(tel):
for i in range(0,len(df)):
if eval(tel[:7])==df.iloc[i]['号码段']:
return [tel,df.iloc[i]['归属地省份'],df.iloc[i]['归属地城市'],df.iloc[i]['卡类型']]
else:
return [tel,'','','']
phones=createPhone()
#print(phones)
alldata=[]
for tel in phones:
alldata.append(GetphoneGuishudi(tel))
dfres1=pd.DataFrame(alldata,columns=['号码','归属地省份','归属地城市','卡类型'])
dfres1.to_csv(r'C:\UsersDesktop\号码归属地查询结果.csv',encoding='gbk')
2、二分法
import pandas as pd
import random
df=pd.read_csv(r'C:\Users\Desktop\号码归属.csv',engine='python')
def createPhone():
phones=[]
for k in range(10):
prelist=["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159","186", "187", "188", "189"]
phone=random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))
phones.append(phone)
return phones
def BinarySearch(phone, df):
try:
df= df.reset_index(drop=True)
# 记录最大最小
min = 0
max = df.shape[0]
center = int((min + max) / 2)
row=df.loc[int((min + max) / 2)]
if int(phone[:7])==row['号码段']:
return [phone,row['号码段'],row['归属地省份'],row['归属地城市']]
elif int(phone[:7])>row['号码段']:
df=df[int((min + max) / 2):max]
return BinarySearch(phone,df)
elif int(phone[:7])
return BinarySearch(phone,df)
else:
return [phone,'','','']
except:
return [phone,'','','']
phones=createPhone()
data=[]
for phone in phones:
data.append(BinarySearch(phone,df))
dfres1=pd.DataFrame(data,columns=['号码','归属地省份','归属地城市','卡类型'])
dfres1.to_csv(r'C:\Users\Desktop\号码归属地查询结果2.csv',encoding='gbk')
3、pandas.merge方法
import pandas as pd
import random
df=pd.read_csv(r'C:\Users\Desktop\号码归属.csv',engine='python')
def createPhone():
phones=[]
for k in range(10):
prelist=["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159","186", "187", "188", "189"]
phone=random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))
subphone=phone[:7]
phones.append([int(phone),int(subphone)])
return phones
phones=createPhone()
df_phones=pd.DataFrame(phones,columns=['号码','号码段1'])
dfres1=pd.merge(df_phones,df,how='left',left_on=['号码段1'],right_on=['号码段'])
dfres2=dfres1.loc[:,['号码','归属地省份','归属地城市','卡类型','区号','邮政编码']]
dfres2.to_csv(r'C:\Users\Desktop\号码归属地查询结果2.csv',encoding='gbk')
4、pandasql pandas数据库查询方法
import pandas as pd
from pandasql import *
import random
df=pd.read_csv(r'C:\Users\Desktop\号码归属.csv',engine='python')
def createPhone():
phones=[]
for k in range(10):
prelist=["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159","186", "187", "188", "189"]
phone=random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))
phones.append([int(phone),int(phone[:7])])
return phones
phones_df=pd.DataFrame(createPhone(),columns=['号码','号码段'])
q="select m.号码段,df.归属地省份 from phones_df m left join df on m.'号码段'= df.'号码段'"
dfres=sqldf(q)
print(type(dfres))
dfres.to_csv(r'C:\Users\Desktop\号码归属地查询结果4.csv',encoding='gbk')