如何仅对 pandas 数据框的最后一行设置样式? [英] How do I style only the last row of a pandas dataframe?

查看:75
本文介绍了如何仅对 pandas 数据框的最后一行设置样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以设置熊猫数据框的样式:

I can style a pandas dataframe:

import pandas as pd
import numpy as np
import seaborn as sns
cm = sns.diverging_palette(-5, 5, as_cmap=True)

df = pd.DataFrame(np.random.randn(3, 4))
df.style.background_gradient(cmap=cm)

但是我不知道如何只将样式应用于最后一行. background_gradient调用中有一个subset选项,它建议我使用

but I can't figure out how to only apply a style to the last row. There is a subset option in the background_gradient call, and it suggests that I use an index slice but I cannot figure out how to make just the last row have any kind of styling.

这是我最接近成功的地方:

Here is my closest to success:

df.style.background_gradient(cmap=cm, subset=[2], axis='index')

推荐答案

使用index的最后一个元素作为子集.

Use the last element of your index as your subset.

df.style.background_gradient(cmap=cm, subset=df.index[-1])



这篇关于如何仅对 pandas 数据框的最后一行设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆