[英]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文件时保持列顺序的内容。
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.