熊猫:AttributeError:“系列”对象没有属性“样式”

问题描述

我正在尝试仅对特定输出进行着色,诸如此类

df['a'] = df['a'].fillna(df['b'].map(s)).style.applymap(lambda x: "background-color:yellow")

所以我得到了错误

AttributeError: 'Series' object has no attribute 'style'

如何正确设置颜色,仅对由此产生的输出进行着色?

解决方法

正如某人大声疾呼的那样,style格式化程序适用于DataFrame,而您正在使用Series。您可以使用.style.to_frame()调用前添加 例如 df['a'] = df['a'].fillna(df['b'].map(s)).to_frame().style.applymap(lambda x: "background-color:yellow")

或者,不太明确地,要使用DataFrame而不是Series,则将您的列名称包装在另一组括号中,例如df[['a']]代替df['a']