import re
phone_regex=re.compile(r'1\d{10}')
import pandas as pd
df=pd.read_excel(r'd:\Users\zhanggl21\Desktop\6月直聊.xlsx')
#定义一个空的列表
alst=[]
#循环并把匹配到的值添加到列表(注册判断空值)
for x in df.聊天记录:
if phone_regex.search(str(x)) is not None:
alst.append(phone_regex.search(str(x)).group())
else:
alst.append(None)
print(alst)
#增加新列
df['聊天号码提取']=alst
#查看
df[list(map(lambda x: str(x).isdigit(),df.聊天号码提取))][['聊天记录','聊天号码提取']].head(10)