How I can save each column of Dataframe to separated column in CSV file?

馋奶兔 提交于 2020-07-09 12:52:23

问题


when I save my dataframe to csv file, It is combined in one column, I want each column of data frame to appear in separated column in CSV file, this is the code

df.to_csv(r'myData.csv',sep=',',encoding="utf-8",columns=['id','created_at','text'])

but it is saved in one column of CSV file, in A column separated by comma

0,2019-09-28 08:58:13,"The TRUTH about Carbon Dioxide"

and I want each value in separated column A,B,C

Is there any way to do that? what I got is

what I want is:

thank you


回答1:


In a csv file, each row is a separate line, and in this row, the different columns are separated by comma.

So

`0,2019-09-28 08:58:13,"The TRUTH about Carbon Dioxide"` 

is a row of the data frame, not a column. The columns are still separated by comma in the csv file.




回答2:


OK the answer is simply changing sep=',' to sep=';'



来源:https://stackoverflow.com/questions/58146464/how-i-can-save-each-column-of-dataframe-to-separated-column-in-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!