1

I am new in Python. I use xlwt so I am having a problem with writing outputs to excel file.

I have an output data like:

[['Alex'], [23, 40], ['Food', 'Clothes']]
[['Andy'], [10, 23], ['Telephone', 'Games']]

I just want data is written like this

  • Does this answer your question? [Writing to an Excel spreadsheet](https://stackoverflow.com/questions/13437727/writing-to-an-excel-spreadsheet) – theherk Dec 14 '20 at 08:08
  • Check this https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html – Dennis Dec 14 '20 at 08:33

1 Answers1

1

It looks that your input is some collections of lists. You can create an empty Dataframe and add each list as new rows separately. See below:

import pandas as pd

df=pd.DataFrame(columns=['Name', 'Amount', 'Spendings'])

for x in your_lists:
    for i in range(len(x[1])):
        df.loc[len(df)]=[x[0][0], x[1][i], x[2][i]]

df.set_index('Name', inplace=True)

>>> print(df)

     Amount  Spendings
Name
Alex     23       Food
Alex     40    Clothes
Andy     10  Telephone
Andy     23      Games
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30