Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inserting list contents into columns instead of rows using Pandas library

My goal is to add lists into three columns and export to excel. I was successful in adding the lists in rows, but the data would suit better in columns.

I added the data to rows using this code:

df = pd.DataFrame(data=[firstList,secondList,thirdList])

df.to_excel('new.xlsx', sheet_name='first sheet')

I am unsuccessful in adding it to columns using this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.DataFrame({
    'col1' : [firstList],
    'col2' : [secondList],
    'col3': [thirdList]
})

df.to_excel('new.xlsx', sheet_name='first sheet')

The output on the code listed above is:
enter image description here

Here is the desired output:
enter image description here
Another thing I am trying to do is remove the indexing column as well, but that is less important

>Solution :

If all list have the same lengths, use:

df2 = df.apply(pd.Series.explode)

NB. If you have string convert first to list:

df = df.apply(pd.eval)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading