I have a dataframe in which one of the columns contains list object like [1,2].
I am trying to export to csv with the following line
df.to_csv('df.csv', sep = ';')
However, the resultant csv, instead of having each row in a single cell, split the row at the comma inside the list object, so I have something like
Column A | Column B |
---|---|
0;xxx;xxx;[1 | 2];xxx;xxx;xx |
Can someone help? Thanks!
What I want is
Column A |
---|
0;xxx;xxx;[1,2];xxx;xxx;xx |
CodePudding user response:
You’ll probably need to surround the list objects with double quotes to make them strings. Then you can use something like this to transform each string back into a list:
import ast
ast.literal_eval(list_as_string)
CodePudding user response:
This problem can be solved by using quotes:
import csv
df.to_csv('df.csv', sep = ',', quoting=csv.QUOTE_ALL)