Python Forum
PANDAS: DataFrame | Replace and others questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PANDAS: DataFrame | Replace and others questions
#1
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?
Larz60+ write Jan-09-2022, 10:46 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
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.
moduki1 likes this post
Reply
#3
Thank you , i will fix it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 860 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,054 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,347 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,214 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,204 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,491 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 8,851 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,370 Jun-22-2021, 07:57 PM
Last Post: snippsat
  Pandas replace function not working on datafram with floats bcrypto 1 2,732 Apr-12-2021, 08:59 PM
Last Post: bcrypto
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,844 Jan-11-2021, 09:53 PM
Last Post: Azureaus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020