Python Forum
PANDAS: DataFrame | Replace and others questions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: PANDAS: DataFrame | Replace and others questions (/thread-36015.html)



PANDAS: DataFrame | Replace and others questions - moduki1 - Jan-09-2022

Hello, i'm trying to create a script that parse the CSV.
This is what i got at moment.

#### Importando arquivo CSV

import pandas as pd

#data = pd.read_csv(r'promocao.csv', encoding='unicode_escape')
#df = pd.DataFrame(columns=['descricao', 'artigo'])

df = pd.read_csv(r"C:\Users\Bruno Nyland\Jupyter\teste.csv",sep=';', encoding= 'utf-8')
df[:10]

#### Ajustando o nome das colunas

df.columns =[x.lower().replace("_","").replace("?","").replace("*","") \
             .replace("¹","").replace("²","").replace("³","") \
             .replace("ã","a").replace("ç","c")for x in df.columns]

df.columns


#### Definindo as colunas.

df.columns = ['modelo', 'referencia', 'ean', 'codncm', 'descricao', 'cor', 'tamanho',
       'valorvenda', 'valorcusto', 'linha', 'artigo', 'genero', 'material',
       'faixa', 'colecao', 'cnpjfornecedor', 'codfranqueadora',
       'unidademedida', 'percroyalties', 'origem', 'marca', 'compartigo',
       'cnpjloja', 'pesobruto', 'pesoliquido', 'placeholder1', 'placeholder2']
df[:10]


#### Validando os dados

####descricao = /*-+.,<>;:\'@#$%¨&"=()+ and strip á é í ó ú  <--- I NEED TO REMOVE THIS CHARACTERS FROM THE STRING

df['modelo'] = df['modelo'].str.replace("/","").str.replace("*","").str.replace("-","").str.replace("+","").str.replace(".","") \
    .str.replace(",","").str.replace("<","").str.replace(">","").str.replace(";","").str.replace(":","") \
    .str.replace("'","").str.replace("@","").str.replace("#","").str.replace("$","").str.replace("%","").str.replace("¨","") \
    .str.replace(""\"","").str.replace("&","").str.replace("""""","").str.replace("=","").str.replace("(","").str.replace(")","") \
    .str.replace("á","a").str.replace("é","e").str.replace("í","i").str.replace("ó","o").str.replace("ú","u") \
    .str.replace("Á","A").str.replace("É","E").str.replace("Í","I").str.replace("Ó","O").str.replace("Ú","U")
    
ERROR:  File "C:\Users\BRUNON~1\AppData\Local\Temp/ipykernel_12440/625730687.py", line 8
    .str.replace(""\"","").str.replace("&","").str.replace("""""","").str.replace("=","").str.replace("(","").str.replace(")","") \
                                                                                                                                ^
SyntaxError: unexpected character after line continuation character                                                                                                                
                                                                                                              

#### Remover espaços em branco

df['modelo'] = df['modelo'].str.strip()
df['referencia'] = df['referencia'].str.strip()
df['descricao'] = df['descricao'].str.strip()


#### Salvar o resultado final

df.to_csv('finalfodastico.csv',sep=';', index=False)
I need to remove that group os special caracters from the string but this error:

Error:
ERROR: File "C:\Users\BRUNON~1\AppData\Local\Temp/ipykernel_12440/625730687.py", line 8 .str.replace(""\"","").str.replace("&","").str.replace("""""","").str.replace("=","").str.replace("(","").str.replace(")","") \ ^ SyntaxError: unexpected character after line continuation character



Y'all have any ideia and suggestions to increase the quality of my code?


RE: PANDAS: DataFrame | Replace and others questions - jefsummers - Jan-10-2022

In line 36 the # makes python regard everything after it in that line to be a comment. Note it is green in the display here.


RE: PANDAS: DataFrame | Replace and others questions - moduki1 - Jan-10-2022

Thank you , i will fix it