I want to write a pandas dataframe to csv. One of the columns of the df has entries which are lists, e.g. [1, 2], [3, 4], ...
When I use df.to_csv(\'output.csv\') and I
Add keyword argument quoting=csv.QUOTE_ALL
:
import csv
import pandas as pd
data = {"A": [1,2,3], "B": [[11,12],[21,22],[31,32]]}
df = pd.DataFrame(data)
df.index.name='index'
# df
# A B
# index
# 0 1 [11, 12]
# 1 2 [21, 22]
# 2 3 [31, 32]
df.to_csv('test.csv',quoting=csv.QUOTE_ALL)
"index","A","B"
"0","1","[11, 12]"
"1","2","[21, 22]"
"2","3","[31, 32]"