Maxime Campeau
Maxime Campeau

Reputation: 109

Splitting a list in Python and inserting it into Excel

I am fairly new to Python here and I have a little challenge. I currently have a list that goes like this :

[
    'Rue des Mélèzes', 36.46629112862116,
    ' ', 63.16043128932904,
    'Rang Saint-André', 50.916698753497634,
    'Avenue de la Sorbonne', 70.59562762879435,
    'Boulevard Jean-Leman', 87.80402184628288
]

PS: the third value is empty and needs to stay like this

where basically the first value is a street name, the second is the computed value of that street, and the process keeps going.

I am looking for a way to take this list and insert it into excel, while having 2 separate columns, 1 for the names and 1 for the values. I assume it is possible since they follow a pattern?

Any help would be tremendously appreciated.

Thanks

Upvotes: 0

Views: 393

Answers (1)

Daniel Labbe
Daniel Labbe

Reputation: 2019

Using pandas:

import pandas as pd

list1 = ['Rue des Mélèzes', 36.46629112862116, ' ', 63.16043128932904, 'Rang Saint-André', 50.916698753497634, 'Avenue de la Sorbonne', 70.59562762879435, 'Boulevard Jean-Leman', 87.80402184628288]

df = pd.DataFrame ()
df['name'] = list1[0::2]
df['value'] = list1[1::2]

df.to_excel('plan1.xlsx', index = False)

It produces the following spreadsheet:

enter image description here

Upvotes: 3

Related Questions