简体   繁体   中英

How to apply a style to a Python DataFrame for all rows except the last one?

I am trying to apply a Bar Style to all of the data in the dataframe, except the last row, which is supposed to be the Total row.

import pandas as pd
import numpy as np
data =  pd.DataFrame(np.random.randn(5, 2), columns=list('AB'))
data.loc['Total'] = data.sum()

           A                B
0      -1.224620    -0.373898
1       0.75568      0.997875
2      -1.284663    -0.211903
3      -0.274813    -0.871816
4       1.256267    -0.742521
Total  -0.772143    -1.202263

It was explained in the docs, that

A tuple is treated as (row_indexer, column_indexer)

You just need to twist a bit the subset option.

On your data

import pandas as pd
import numpy as np
data =  pd.DataFrame(np.random.randn(5, 2), columns=list('AB'))
data.loc['Total'] = data.sum()

data.style.bar(subset = ([0,1,2,3,4], ['A', 'B']))

it gives

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM