带有电话号码库的验证电话号码列python pandas

问题描述

我想从我的数据框中使用电话号码库检查多个电话号码 https://pypi.org/project/phonenumbers/

我想验证电话号码,最终我想知道这个号码来自哪个国家。例如:

联系方式 电话号码 电话查询 手机国家
1 31650868016 真的 荷兰
2 447986123456 真的 英国
3 55677 错误

我使用了这个解决方案:https://stackoverflow.com/a/56782746 我做了一个 Country 专栏。

但我想使用 phonenumbers.is_valid_number() 函数,最终使用 geocoder.description_for_number() 函数

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

错误:AttributeError:'Series' 对象没有属性 'phoneNumber'

解决方法

导致此错误的具体问题是 axis 之后而不是之前的错位括号:

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

我认为下面是您要寻找的更多内容:

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

我使用 None 作为占位符,因为您的 DataFrame 中没有国家/地区代码。