pandas 处理pandas Data Frame列Name中的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47719494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dealing with special characters in pandas Data Frame′s column Name
提问by user1922364
I am importing an excel worksheet that has the following columns name:
我正在导入一个具有以下列名称的 Excel 工作表:
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 No 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
采纳答案by Evan
Thanks for posting the link to the Google Sheet. I downloaded it and loaded it via pandas:
感谢您发布指向 Google 表格的链接。我下载了它并通过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('o', '')
Note that the two replace
statements are replacing different characters, although they look very similar.
请注意,这两个replace
语句正在替换不同的字符,尽管它们看起来非常相似。
Help from: Why do I get a SyntaxError for a Unicode escape in my file path?
回答by Alex Zisman
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)