Reputation:
I have df
day
2016-03-01 [00051f002f5a0c179d7ce191ca2c6401, 00102b98bd9...
2016-03-02 [00102b98bd9e71da3cf23fd1f599408d, 0012ea90a6d...
2016-03-03 [00051f002f5a0c179d7ce191ca2c6401, 00102b98bd9...
I want write it to excel
, but afterwards I get
day
2016-03-01 "['00051f002f5a0c179d7ce191ca2c6401' '00102b98bd9e71da3cf23fd1f599408d'
'00108f5c5de701ac4386e717a4d07d5b' ..., 'null' 'test017' 'undefined']"
2016-03-02 "['00102b98bd9e71da3cf23fd1f599408d' '0012ea90a6deb4eeb2924fb13e844136'
'0019b08bc9bb8da21f3b8ecc945a67d3' ..., 'test4' 'undefined'
'xx6da37101dffabe00e5d636c01719b6']"
2016-03-03 "['00051f002f5a0c179d7ce191ca2c6401' '00102b98bd9e71da3cf23fd1f599408d'
'0012ea90a6deb4eeb2924fb13e844136' ..., 'test017' 'undefined'
'xx6da37101dffabe00e5d636c01719b6']"
I used df.to_excel('name.xlsx')
. I don't understand, why it wrote the list in the second column without commas. I did it earlier and it worked as expected, but now it doesn't.
Upvotes: 1
Views: 214
Reputation: 1195
You should be able to use:
df['ColumnID'] = df['ColumnID'].astype(str)
on your column with the list to treat it as a string, which should preserve the commas.
Here is a full working example:
# create sample dataframe
df = pd.DataFrame({'day':[1,2,3],'list':[['a','b','c'],['d','e','f'],['g','h','i']]})
# convert list column to string
df['list'] = df['list'].astype(str)
# export excel file
writer = pd.ExcelWriter("out.xlsx", engine = 'xlsxwriter')
df.to_excel(writer, sheet_name="data")
writer.save()
Which results in the following excel file:
Upvotes: 1