【问题标题】:Pandas: AttributeError: 'Series' object has no attribute 'style'熊猫:AttributeError:“系列”对象没有属性“样式”
【发布时间】:2020-08-23 11:24:28
【问题描述】:

我正在尝试为某个输出着色,类似这样

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

所以我得到了错误

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

我怎样才能使它正确并仅对由此产生的输出着色?

【问题讨论】:

  • 这可能会有所帮助,stackoverflow.com/questions/41654949/…
  • 不,这不是我要找的,这会突出显示我不需要的整列,我需要突出显示由某个代码打印的单元格
  • 请详细说明您的问题..提供一个虚拟的df 示例。 style 函数也适用于 DataFrame,而您正在申请 Series

标签: python pandas series pandas-styles


【解决方案1】:

正如有人所说,style 格式化程序用于 DataFrames,而您正在使用一个系列。您可以在 .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']

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 2016-03-27
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2019-10-02
    • 1970-01-01
    相关资源
    最近更新 更多