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
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')

#### Ajustando o nome das colunas

df.columns =[x.lower().replace("_","").replace("?","").replace("*","") \
             .replace("¹","").replace("²","").replace("³","") \
             .replace("ã","a").replace("ç","c")for x in 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']

#### 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") \
ERROR:  File "C:\Users\BRUNON~1\AppData\Local\Temp/ipykernel_12440/", 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: File "C:\Users\BRUNON~1\AppData\Local\Temp/ipykernel_12440/", 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.
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
Thank you , i will fix it

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 475 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,331 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,268 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,582 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,545 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,386 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,683 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,821 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,658 Jun-22-2021, 07:57 PM
Last Post: snippsat
  Pandas replace function not working on datafram with floats bcrypto 1 3,170 Apr-12-2021, 08:59 PM
Last Post: bcrypto

Forum Jump:

User Panel Messages

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