Search code examples
pythonpandasholoviews

HoloViews Pandas Pivot displayed in HV Layout


I have a pandas df that I have pivoted to give me the view below, which is a count of categorical data:

Status       Green     Red      Yellow  
Brand        
B1           20        2        10
B2           10        4         0
B3            5       10         4

Is there a way i can have this table converted to a hv.Table? I've tried a few different ways, including making my initial dataset a HV.Dataset, but keep getting passed with errors.

I would just like to include the datatable with its accompanying chart in a report that I am building.

Thank you in advance for any help/suggestions.

Kind regards,


Solution

  • If I do something like this I get a df that looks like yours, but hv.Table() doesn't give any errors, so like this there doesn't seem to be a problem:

    # import libraries
    import numpy as np
    import pandas as pd
    import holoviews as hv
    hv.extension('bokeh')
    
    # create sample data
    status = np.random.choice(['Green', 'Yellow', 'Red'], size=100)
    brand = np.random.choice(['B1', 'B2', 'B3'], size=100)
    df = pd.DataFrame({'brand': brand, 
                       'status': status})
    
    # create same grouped data as in the question
    grouped = df.groupby(['brand', 'status'])['status'].count().unstack().reset_index()
    
    # create holoviews table from the grouped data
    hv.Table(grouped)