Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!