Pandas style object with multi-index

前端 未结 4 1347
广开言路
广开言路 2020-12-16 17:25

I am formatting a pandas dataframe with styler to highlight columns and format numbers. I also want to apply multi-index for more clear, pleasant and easy to read. Since I a

相关标签:
4条回答
  • 2020-12-16 17:46

    For the original question:

    data.style.background_gradient(cmap=cm,
                                   subset=[c for c in data.columns if c[1] == 'A'])
    

    You could check some condition, substring for example:

    data.style.background_gradient(cmap=cm,
                                   subset=[c for c in data.columns if 'T' in c[0]])
    

    Or pass a list of subcolumns:

    data.style.background_gradient(cmap=cm, 
                                   subset=[c for c in data.columns if c[1] in ('A', 'C')])
    

    0 讨论(0)
  • 2020-12-16 17:54

    I think you can use pd.IndexSlice[...] method:

    data.style.background_gradient(cmap=cm, subset=pd.IndexSlice[:, pd.IndexSlice[:, 'A']])
    

    Demo:

    In [5]: data.loc[pd.IndexSlice[:, pd.IndexSlice[:, 'A']]]
    Out[5]:
            One
              A
    0 -0.808483
    1  0.009371
    2  0.977138
    3 -0.875554
    4 -0.052424
    
    In [6]: data
    Out[6]:
            One                 Two
              A         B         C         D
    0 -0.808483 -2.280683  0.576145  0.649688
    1  0.009371  0.721510  1.013764 -0.157493
    2  0.977138  1.441392  1.718618 -0.320826
    3 -0.875554 -1.060507  1.457075  0.570195
    4 -0.052424 -0.742842 -0.203830 -1.202091
    

    in Jupyter:

    0 讨论(0)
  • 2020-12-16 17:54

    If you know what the hierarchy of the index is, for example that 'A' is under 'One' you can use a tuple to reference the column.

    data.style.background_gradient(cmap=cm, subset=[('One','A')])
    

    Then the table will be displayed as above.

    Should you want to style more than one column with a multi-index you need to provide a list of tuples ie

    arrays = [np.hstack([['One']*2, ['Two']*2]) , ['A', 'B', 'C', 'D']]
    columns = pd.MultiIndex.from_arrays(arrays)
    data =  pd.DataFrame(np.random.randn(5, 4), columns=list('ABCD'))
    data.columns = columns 
    cm = sns.light_palette("green", as_cmap=True)
    data.style.background_gradient(cmap=cm, subset=[('One','A'),('Two','C')])
    

    Displays like this

    0 讨论(0)
  • 2020-12-16 18:00

    Here, is another way:

    data.style.background_gradient(cmap=cm, subset=data.columns.get_loc_level('A', level=1)[0])
    

    Output:

    0 讨论(0)
提交回复
热议问题