[英]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)
元组被视为(row_indexer,column_indexer)
You just need to twist a bit the subset option. 您只需要稍微扭曲一下subset选项。
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 它给
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.