简体   繁体   English

如何为除最后一行以外的所有行将样式应用于Python DataFrame?

[英]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.

相关问题 沿着 DataFrame 中的行处理重复项并删除除 Python Pandas 中的最后一个之外的所有行 - Working through duplicates along rows in DataFrame and deleting all except the last one in Python Pandas 如何删除DataFrame除第一行和最后一行外的所有行 - How to delete all the rows of the DataFrame except the first row and the last row 如何将操作应用于 Dataframe 的所有列名,除了 Pandas 中的一个? - How to apply an operationg to all column names of a Dataframe except one in Pandas? 如何在 dataframe 中使用 loc 来获取除 python 中最后一个之外的所有列值? - how to use loc in dataframe to get all values of columns except last one in python? 如何将DataFrame中除第一列之外的所有列合并为一列,并删除空行? 蟒蛇 - How to merge all columns in a DataFrame except the first into one column and drop empty rows? Python 除了一个特定的行,如何划分熊猫Dataframe中的所有行? - How to divide all rows in a panda Dataframe except for one specific row? 将数据框的所有行与其他行合并在一起 - Club all the rows of a dataframe as others except one 应用于除最后一行之外的所有行的应用函数 - Apply function that applies to all rows except last row 修改组的所有行,除了 Dataframe 中的最后一行 - modify all rows of a group except the last row in a Dataframe 向前填充python pandas数据帧中除最后一个值之外的所有内容 - Forward fill all except last value in python pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM