1

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.

5
  • What exactly is incorrect and what is the desired output? Commented May 24, 2016 at 15:25
  • I want that will be list and elem should be delimited with ,, like in df Commented May 24, 2016 at 15:30
  • So does your dataframe have two columns one with a day, the other containing a list? Commented May 24, 2016 at 15:30
  • I did it earlier and it worked Commented May 24, 2016 at 15:32
  • yes, one is day, second contain a list Commented May 24, 2016 at 15:32

1 Answer 1

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

How can I give the name to this column? I do df = df.set_index('mm_id', drop=True).transpose() df = df.apply(lambda row: row[row != 0].index.tolist(), axis=1)
what is the output of df.columns?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.