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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.