Home > Blockchain >  How to use phonenumbers Python library on a pandas dataframe testing phone field before?
How to use phonenumbers Python library on a pandas dataframe testing phone field before?

Time:10-24

I would to parse a phone field in a pandas dataframe. My phone field is named Phone and my country code field CountryCode.

It works but only when all the phones are filled:

df['Test phone'] = df.apply(lambda x:phonenumbers.parse(str(x['Phone']), str(x['CountryCode'])), axis='columns')

OK, but how to do it only if the phone, or even the country code, are filled? I tried a lot of if/else syntaxes in the lambda function, without success.

Amazing, I also tried doing it in a df.loc, it does not work neither...

Please, have you an idea?

THX

CodePudding user response:

Just use an if and else statement within your lambda and test with row.notnull().all() if all row entries are not null.

I added an example from the phonenumbers project.


import numpy as np
import pandas as pd
import phonenumbers

df = pd.DataFrame(
    [
        [
            np.nan,
            "020 8366 1177",
        ],
        ["GB", "020 8366 1177"],
        [np.nan, np.nan],
    ],
    columns=["CountryCode", "Phone"],
)

df["Test phone"] = df.apply(
    lambda row: phonenumbers.parse(str(row["Phone"]), str(row["CountryCode"]))
    if row.notnull().all()
    else "",
    axis="columns",
)

Output:

    CountryCode Phone   Test phone
0   NaN 020 8366 1177   
1   GB  020 8366 1177   Country Code: 44 National Number: 2083661177
2   NaN NaN 

CodePudding user response:

Thank you

Your code do not work as it, but you very helped me: it was a syntax error.

From your code, now I can do:

df['Phone'] = df['Phone'].fillna('')
df['CountryCode'] = df['CountryCode'].fillna('')

df['TestPhone'] = df.apply(
    lambda x: phonenumbers.parse(str(x['Phone']), str(x['CountryCode']))
    if str(x['Phone']) != ''
    and str(x['CountryCode']) != ''
    else '',
    axis='columns'
)
  • Related