Preserving column order in the pandas to_csv method

后端 未结 3 1926
走了就别回头了
走了就别回头了 2021-02-13 09:16

The to_csv method of pandas does not preserve the order of columns. It chooses to alphabetically arrange the columns in CSV. This is a bug and has been reported and is supposed

3条回答
  •  情书的邮戳
    2021-02-13 09:53

    Try with:

    df.to_csv(file_name, sep=',', encoding='utf-8', header=True, columns=["Col1","Col2","Col3","Col4"])
    

    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html

提交回复
热议问题