Search code examples
pythonphone-number

Applying Phonenumbers function to Series


I have the following code which parses through some text and outputs the international phone number.

IN[]:
import phonenumbers
text = "02-2947145 Call this +639154764945"

for match in phonenumbers.PhoneNumberMatcher(text, "PH"):
    print(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164))

OUT[]:
+6322947145
+639154764945

I need to apply this code to a series within a dataframe, like such

ID    Phone
1     +639154764945
2     00639154764945
3     09154764945 or call this +639154764944
4     00639154764945

I would like these to output as such

ID    Phone_clean
1     +639154764945
2     +639154764945
3     +639154764945, +639154764944
4     +639154764945

I.e. When two phone numbers are identified, they are outputted as comma separated values


Solution

  • #  Defining a function to convert phone numbers to International format
    def phone_cleaner(fc_lead_phone):
        for match in phonenumbers.PhoneNumberMatcher(fc_lead_phone,"PH"):
            return phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
    
    leads['phone_one_clean'] = leads['phone_one'].apply(phone_cleaner)