Home > database >  using lambda to extract the number expected
using lambda to extract the number expected

Time:01-02

Happy New Year all, I have a list of numberphone got from an another function , however , it still exist a lot of unexpected number , sth like below:

['0355198232', '0963.297.187', '0355198232', '0355198232', '000099772009', '0355198232']

the unexpected is '000099772009' , i tried to wrote the function to do this

a = '0098475634859'
b = '0903.164.570'
def isphone(num):
    if len(num) > 10:
        if re.findall('[ .]',num):
            return num
df['Phone Number 2'] = df['Phone Number'].apply(lambda x: isphone(str(x)))

This function work correctly with "a" , "b" variable, however when i applied it into the dataframe with lambda ,it seems that not work well. Can someone please help assist? update my lab : I have dataframe with column "Phone Number" like below:

enter image description here

CodePudding user response:

Looking at the screenshot, it looks like you have lists of phone numbers in "Phone Number" column, so you could try this:

import re

import pandas as pd

df = pd.DataFrame(
    {
        "Phone Number": [
            [
                "0355198232",
                "0963.297.187",
                "0355198232",
                "0355198232",
                "000099772009",
                "0355.198.232",
            ],
        ]
    }
)

df["Phone Number 2"] = df["Phone Number"].apply(
    lambda x: [isphone(str(phone)) for phone in x]
)

And then:

print(df["Phone Number"][0])
# ['0355198232', '0963.297.187', '0355198232', '0355198232', '000099772009', '0355.198.232']

print(df["Phone Number 2"][0])
# [None, '0963.297.187', None, None, None, '0355.198.232']
  • Related