在Python中需要帮助,为虚构的联系人列表中的错误值打印一个空白字段



我需要帮助了解如何在联系人列表中为不正确的值打印空白字段(中的硬接线(。以下是我努力实现目标的一个例子。

[In] contacts = [['Mike Green',   '21873', '5555555555'],
['Sage', '57105', '605414147']]
Currently printing:
[Out]               Name        Zip         Phone
0             Mike Green      21873  555-555-5555
1                   Sage      57105     605414147
Needing it to print this instead:
[Out]               Name        Zip         Phone
0             Mike Green      21873  555-555-5555
1                   Sage      57105     

这是我的代码和我目前键入的内容:

import pandas as pd
import re
contacts = [['Mike Green',   '21873', '5555555555'],
['Donette Foller', '45011', '5135701893'],
['Mitsue Tollner', '60632', '7735736914'],
['Leota Dilliard', '95111', '4087523500'],
['Sage', '57105', '605414147'],
['Helen Cooper', 'asxa23245', '3421322323'],
['Jim Roberts90on', '21801', '5555555555'],
['J324osephine Darakjy', '48116', '8103749840'],
['Venere', '08014', '8562644130'],
['Lenna Paprocki', '99501', '9073854412'],
['Donette Foller', '45011', '5135701893'],
['Simona Morasca', '44805', '4195032484'],
['Mitsue', '60632', '7735736914'],
['Leota Dilliard', '95111', '4087523500'],
['Sage Wieser', '57105', '6054142147'],
['Helen Cooper', '23245', '3421222323'],
['Robertson', 'ssasx-21', '5555555555'],
['Josephine Darakjy', '48116', '8103749840'],
['Art Venere', '', '2644130'],
['Paprocki', '99501', '9073854412']]
contactsdf = pd.DataFrame(contacts,columns=['Name', 'Zip', 'Phone'])
def get_formatted_phone(value):
result = re.fullmatch(r'(d{3})(d{3})(d{4})',   value)
return '-'.join(result.groups()) if   result else value
formatted_phone = contactsdf['Phone'].map(get_formatted_phone)
contactsdf['Phone'] =   formatted_phone
print(contactsdf)

如果您只想要一个空白字段,那么只需将get_formatted_phone函数中的值更改为:

def get_formatted_phone(value):
result = re.fullmatch(r'(d{3})(d{3})(d{4})',   value)
return '-'.join(result.groups()) if   result else ''

要以类似的方式格式化邮政编码,假设正确的值总是5个字符长,而不正确的值则不会,您可以使用以下方法:

def get_formatted_zip(value):
return value if len(value) == 5 else ''
contactsdf['Zip'] = contactsdf['Zip'].map(get_formatted_zip)

如果不正确的Zip值不那么容易被发现,那么你可以使用这样的东西:

def get_formatted_zip(value):
try:
if len(str(int(value))) == 5:
return value
else:
return ''
except:
return ''

最新更新