Search code examples
pythonpandasweb-scrapingindexingscalar

how to solve "If using all scalar values, you must pass an index" problem pandas


Following my previous question , all is working well but when the list it's long it's showing this erreur, This is a simple of d :

['Houda Golf & Aquapark Novostar Monastir ', " {'All Inclusive soft': ('202', '175', '15%'), 'Demi Pension': ('161', '140', '15%'), 'Petit Dejeuner': ('137', '119', '15%'), 'DP plus': ('237', '119', '15%')}"]

Anyone knows how can i fix it ? thank you in advance.

NB: I've tried several solutions from responses here but they hasn't work for me

enter image description here


Solution

  • Try to convert the values of dictionary to list if they are scalars:

    from ast import literal_eval
    
    
    vals = literal_eval(d[1].strip())
    df = pd.DataFrame(
        {k: v if isinstance(v, (list, tuple)) else [v] for k, v in vals.items()}
    )
    print(df)