Separate extension name - in Python

Asked

Viewed 54 times

-1

I have a column in the spreadsheet in Excel with the following column:

Atendido por 
Ismael  (100)
JEFFERSON LUIZ ESTEVAO DE MOURA (111)
Felipe Braga Regis Souza (222)

I have to take just the name and put in a new column and the extension in another, if the example below:

ANALISTA                                         RAMAL

Ismael                                           100
JEFFERSON LUIZ ESTEVAO DE MOURA                  111
Felipe Braga Regis Souza                         222

My code:

arquivoxlsx = pd.read_excel('C:/Users/Desktop/Python/Base/Atendidas.xlsx')

Atendidapor = arquivoxlsx["Atendida por"]
cont = 0
indice = 0

for c in Atendidapor:
    Analista = arquivoxlsx.at[cont,'Atendida por']
    tamanhomatriz = len(Analista.split())


    # print(registro)


    while True:
        registro = Atendidapor.get(indice)
        if indice < tamanhomatriz:
            arquivoxlsx.at[cont, 'Analista'] = registro + ""
        else:
            arquivoxlsx.at[cont, 'Analista'] = "" + registro
            indice = 0
            break
        indice  += 1
    #
    cont += 1

2 answers

1

You can extract the values directly through the methods Extract and replace of Series pandas.

import pandas as pd

data = ['Ismael  (100)', 'JEFFERSON LUIZ ESTEVAO DE MOURA (111)', 'Felipe Braga Regis Souza (222)']
df = pd.DataFrame(data, columns=['Atendido por'])
df['Analista'] = df['Atendido por'].str.replace(r"\((.*)\)", "")
df['Ramal'] = df['Atendido por'].str.extract(r"\((.*)\)")
df.drop(df.columns[[0]], axis=1, inplace=True)

Out[3]:
                           Analista Ramal
0                          Ismael     100
1  JEFFERSON LUIZ ESTEVAO DE MOURA    111
2         Felipe Braga Regis Souza    222

0

You can use the split by parenthesis so you will have a list where the first element will be the name and the second element the number with the parentheses after is just you include each value in your proper column.

   for x in Atendidapor.index:
        lista = Atendidapor[x].split("(")
        arquivoxlsx['ANALISTA'][x] = lista[0]
        arquivoxlsx['RAMAL'][x] = lista[1].replace("(", "").replace(")", "")

Browser other questions tagged

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