Extract phone number with API in Python pandas

Asked

Viewed 517 times

2

I have an API that extracts the phone number. It works as follows. By passing a number on it, returns me 3 variable type string containing phone with country code, type if it is cellular or fixed and the

telefone,tipo,ddd = extract(numero=34900001111).get_numero()

But I need to take all the numbers in a column in a dataframe and create 3 other columns with the strings returned for each number in that column. So I tried to do a function to use the API as follows:

def telefone():
for i in df2['TELEFONE']:
    telefone,tipo,ddd = extract(numero=i).get_numero()
    df2['TEL'] = telefone
    df2['TIPO'] = tipo
    df2['DDD'] = ddd

So I did it by taking a number and creating the 3 columns, but always with the same output for all rows. Can someone help me figure out where I’m going wrong?

1 answer

2


It is usually possible to do operations on Pandas without using the command for, making use of something called "vectorization".

See if this works:

import numpy as np

# separando a extração de cada variável

@np.vectorize
def telefone(num):

    return extract(numero=num).get_numero()[0]

@np.vectorize
def tipo(num):

    return extract(numero=num).get_numero()[1] 

@np.vectorize
def ddd(num):

    return extract(numero=num).get_numero()[2]



# criando as colunas novas


df2['TEL']  = telefone(df2['TELEFONE'])

df2['TIPO'] = tipo(df2['TELEFONE'])

df2['DDD']  = ddd(df2['TELEFONE'])
  • 1

    Then I want to take a closer look to understand how this numpy. But you’re damn right it worked. Thank you

  • And in case I have 2 parameters , how will be? Types need to pass the Phone and DDD, to create the same columns?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.