Keeping columns in the specified order when using UseCols in Pandas Read_CSV

前端 未结 2 1384
慢半拍i
慢半拍i 2020-12-11 15:11

I have a csv file with 50 columns of data. I am using Pandas read_csv function to pull in a subset of these columns, using the usecols parameter to choose the ones I want:<

相关标签:
2条回答
  • 2020-12-11 15:26

    Just piggybacking off this question here (hi from 2018).

    I discovered the same problem with my pandas read_csv and wanted to figure out a way to take the [col_reorder] using column header strings. It's as simple as defining an array of strings to use.

    pd.read_csv(filepath, index_col=False, usecols=cols_to_use)[index_strings]
    
    0 讨论(0)
  • 2020-12-11 15:36

    you can reuse the same cols_to_use list for selecting columns in desired order:

    df_ret = pd.read_csv(filepath, index_col=False, usecols=cols_to_use)[cols_to_use]
    
    0 讨论(0)
提交回复
热议问题