简体   繁体   English

python:pandas - 数据框列的顺序

[英]python: pandas - order of data frame column

I have following code to output a dataFrame:我有以下代码来输出数据帧:

output = pd.DataFrame({"id":id_test, "hum":y_pred})
output.to_csv("myOutput.csv", index=False)

Then in myOutput.csv, I got hum as the first column, id as the second column.然后在 myOutput.csv 中,第一列是hum ,第二列是id Is there a way to make id the first column instead?有没有办法让id成为第一列? Thanks!谢谢!

Just reorder the columns:只需重新排序列:

output.ix[:,['id','hum']].to_csv("myOutput.csv", index=False)

Because you used a dict as the data, the column order is not necessarily the same order as the key creation order in the dict因为你用的是dict作为数据,列顺序不一定和dict中key的创建顺序一样

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

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