Search code examples
pythonpandasholoviewshvplot

hvplot interactive pd.DataFrame with multiIndex


I'd like to create an interactive hv.plot out of a pd.DataFrame with a multiIndex.

When i try to make my dataframe interactive with df.interactive, i get the error-message:

Supplied data does not contain specified dimensions, the following dimensions were not found: ['fu', 'fu', 'bar', 'bar']

PandasInterface expects tabular data, for more information on supported datatypes see http://holoviews.org/user_guide/Tabular_Datasets.html

here's the code so far

    dictionary = {('fu', 'A'):[12,13,14,15],
                  ('fu', 'B'):[17,18,19,20],
                  ('bar', 'A'):[35,36,37,38], 
                  ('bar', 'B'):[45,46,47,48]}

    df = pd.DataFrame(dictionary, index=[1,2,3,4])

that's the dataframe I'd like to plot, giving me the following output:

    fu          bar
    A   B   A   B
1   12  17  35  45
2   13  18  36  46
3   14  19  37  47
4   15  20  38  48

Any ideas how to go on from here?


Solution

  • IIUC you could flatten the columns (reduce the two column levels to a simple index) before invoking the hvplot methods:

    df_flat = df.set_axis(map(" ".join, df.columns), axis=1)
    
    print(df_flat)
    
    # output:
    #     fu A  fu B  bar A  bar B
    #  1    12    17     35     45
    #  2    13    18     36     46
    #  3    14    19     37     47
    #  4    15    20     38     48
    
    
    df_flat.interactive()