Preserving column order in Python Pandas DataFrame

前端 未结 3 2021
南笙
南笙 2020-12-03 06:22

Is there a way to preserve the order of the columns in a csv file when read and the write with Python Pandas? For example, in this code

import pandas as pd

         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 07:06

    Another workaround is to do this:

    import pandas as pd
    data = pd.read_csv(filename)
    data2 = df[['A','B','C']]  #put 'A' 'B' 'C' in the desired order
    data2.to_csv(filename)
    

提交回复
热议问题