简体   繁体   English

Python Pandas read_csv保持列顺序

[英]Python Pandas read_csv Keep Column Order

I am reading a CSV file into pandas: 我正在将一个CSV文件读入熊猫:

df = pd.read_csv('file.csv')

However, I notice that column order is not preserved. 但是,我注意到不保留列顺序。 I can't find anything in the docs that explain how to keep the column order when reading in the CSV file. 我在文档中找不到任何解释如何在读取CSV文件时保持列顺序的内容。

read.csv convert tabular data into DataFrame object. read.csv将表格数据转换为DataFrame对象。 Since DataFrame are kwargs dictionary, order is not persevered. 由于DataFramekwargs字典,因此订单不会持续。

Sources from DataFrame and read.csv 来自DataFrameread.csv的代码

you can pass list of the columns with the order you would like them to have: 您可以按照您希望他们拥有的顺序传递列的列表:

columns = ['a', 'x', 'b', 'y']
df = pd.read_csv('file.csv', usecols=columns)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM