简体   繁体   中英

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. Is there a way to make id the first column instead? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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