user1922364
user1922364

Reputation: 573

Dealing with special characters in pandas Data Frame´s column Name

I am importing an excel worksheet that has the following columns name:

N° Pedido
   1234
   6424
   4563

The column name ha a special character (°). Because of that, I can´t merge this with another Data Frame or rename the column. I don´t get any error message just the name stays the same. What should I do?

This is the code I am using and the result of the Dataframes:

    import pandas as pd
    import numpy as np
    # Importando Planilhas
    CRM = pd.ExcelFile(r'C:\Users\Michel\Desktop\Relatorio de 
    Vendas\relatorio_vendas_CRM.xlsx', encoding= 'utf-8')
    protheus = pd.ExcelFile(r'C:\Users\Michel\Desktop\Relatorio de 
    Vendas\relatorio_vendas_protheus.xlsx', encoding= 'utf-8')
    #transformando em Data Frame
    df_crm = CRM.parse('190_pedido_export (33)')
    df_protheus = protheus.parse('Relatorio de Pedido de Venda')]
    # Transformando Campos em float o protheus
    def turn_to_float(x):
    return np.float(x)

    df_protheus["TES"] = df_protheus["TES"].apply(turn_to_float)
    df_protheus["Qtde"] = df_protheus["Qtde"].apply(turn_to_float)
    df_protheus["Valor"] = df_protheus["Valor"].apply(turn_to_float)
    #Tirando Tes de não venda do protheus
    # tirando valores com código errado 6
    df_protheus_1 = df_protheus[df_protheus.TES != 513.0]
    df_protheus_2 = df_protheus_1[df_protheus_1.TES != 576.0]

    **df_crm.columns = df_crm.columns.str.replace('N° Pedido', 'teste')
    df_crm.columns**


    Orçamento Origem    N° Pedido   Nº Pedido ERP   Estabelecimento Tipo de 
    Pedido  Classificação(Tipo) Aplicação   Conta   CNPJ/CPF    Contato ... 
    Aprovação Parcial   Antecipa Entrega    Desconto da Tabela de Preço 
    Desconto do Cliente Desconto Informado  Observações Observações NF  Vl 
    Total Bruto Vl Total    Completo
    0   20619.0 23125   NaN Optitex 1 - Venda   NaN Industrialização/Revenda    
    XAVIER E ARAUJO LTDA ME 7970626000170   NaN ... N   N   0   0   0   

Note that I used other codes for the bold part with the same result:

#renomeando tabela para dar Merge
#df_crm['proc'] = df_crm['N\xc2\xb0 Pedido']

#df_crm['N Pedido'] = df_crm['N° Pedido']
#df_crm.drop('N° Pedido',inplace=True,axis=1)
#df_crm

#df_crm['N Pedido'] = df_crm['N° Pedido']
#df.drop('N° Pedido',inplace=True,axis=1)
#df_crm

#df_crm_1 = df_crm.rename(columns={"N°Pedido": "teste"})
#df_crm_1

Upvotes: 3

Views: 21951

Answers (2)

Evan
Evan

Reputation: 2151

Thanks for posting the link to the Google Sheet. I downloaded it and loaded it via pandas:

df = pd.read_excel(r'~\relatorio_vendas_CRM.xlsx', encoding = 'utf-8')
df.columns = df.columns.str.replace('°', '')
df.columns = df.columns.str.replace('º', '')

Note that the two replace statements are replacing different characters, although they look very similar.

Help from: Why do I get a SyntaxError for a Unicode escape in my file path?

Upvotes: 4

Alex Zisman
Alex Zisman

Reputation: 411

I was able to copy the values into another column. You could try that

df['N Pedido'] = df['N° Pedido']
df.drop('N° Pedido',inplace=True,axis=1)

Upvotes: 1

Related Questions