Pandas – save to csv replaces columns

I have problem with saving pandas DataFrame to csv. I run code on jupyter notebook and everything works fine. After runing the same code on server columns values are saved to random columns…

csvPath = r''+str(pathlib.Path().absolute())+ '/products/'+brand['file_name']+'_products.csv' productFrame.to_csv(csvPath,index=True) 

I’ve print DataFrame before saving – looks as it should be. After saving, I open file and values ale mixed.

How to make it always work in the proper way?

Add Comment
1 Answer(s)

If you want to force the column order when exporting to csv, use

df[cols].to_csv() 

where cols is a list of column names in the desired order.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.