Preserving column order in the pandas to_csv method

后端 未结 3 1927
走了就别回头了
走了就别回头了 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:46

    Try the following solution. Even I faced the same issue. I solved it as follows:

    import pandas as pd
    df = pd.DataFrame({'V_pod_error' : [a],
                       'V_pod_used' : [b],
                       'U_sol_type' : [c]
                                    ...
                                    ... and so on upto 50 columns }
    
    column_order = ['V_pod_error', 'V_pod_used', 'U_sol_type',.....# upto 50 column names]
    
    df[column_order].to_csv(file_name)
    

提交回复
热议问题