pythonseleniumoutputxlwt

Writing output to excel file in python


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


Solution

  • 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