[英]Pandas Write to Excel rearranging columns based on alphabetic order
我有一個具有格式的python字典
dict = {
D:""
B:""
A:""
C:""
}
但是,當我將此字典寫入excel中的csv文件時,列重新排列為
A B C D
寫入Excel時,如何在python中保持字典的順序?
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)
上面是寫到excel的代碼。
與上面提到的相反,pandas.DataFrame.to_excel()函數具有一個參數,可讓您設置將哪些訂單列寫入excel工作表。 參數是
columns=[listOfColumns]
對於上面的例子,我會做
list_of_detected_words.to_excel(writer, "Sheet1", startrow=1, columns=[D,B,A,C] )
可以在以下鏈接上找到文檔。
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.