Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
Pandas Write to Excel rearranging columns based on alphabetic order

I have a python dict that has the format

dict = {
        D:""
        B:""
        A:""
        C:""
}

However, when I write this dict to a csv file in excel, the columns are rearranged to

A B C D

How do I keep the order of my dict in python when I write to excel?

    writer = pd.ExcelWriter('list_of_detected_words.xlsx', engine='xlsxwriter')
    list_of_detected_words = pd.DataFrame.from_records(form_info)
    list_of_detected_words.to_excel(writer, "Sheet1",startrow=1)

Above is the code that writes to excel.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Contrary to what was mentioned above, the pandas.DataFrame.to_excel() function has a parameter that lets you set which order columns are written to your excel sheet. The parameter is

 columns=[listOfColumns]

For my example above I would do

list_of_detected_words.to_excel(writer, "Sheet1", startrow=1, columns=[D,B,A,C] )

Documentation can be found on the following link.

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda