首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用熊猫在Excel中应用条件格式不起作用

使用熊猫在Excel中应用条件格式不起作用
EN

Stack Overflow用户
提问于 2022-09-21 02:57:27
回答 1查看 76关注 0票数 2

我已经花了几个小时尝试所有可能的熊猫风格的方法,为Excel表格的条件格式,但没有一个工作.下面有一个这样的数据。

代码语言:javascript
复制
         Category      Month   Sales    Margin  Margin_Rate
0              TV       July   50000  10354.64     0.207093
1          Steros     August   45000  39379.38     0.875097
2          Phones  September   22000   8130.23     0.369556
3     Cell Phones    October   90000   8089.23     0.089880
4  Video Consoles   November   12000   8564.69     0.713724
5     Video Games   December    7000   3187.74     0.455391
6       Wearables    January   28000   8470.51     0.302518
7         Tablets   February   75000   8649.36     0.115325
8          Movies      March   25000  20372.73     0.814909
9         Laptops      April  100000  62309.34     0.623093

(1)。我正在尝试使用熊猫的style.format()进行数字格式化,但它从未起作用。(2)。尝试根据使用style.apply或style.applymap的条件应用单元格背景颜色,而且它也从未起作用。

这是我的代码:

代码语言:javascript
复制
# First of all I tried style.format(precision=1)
df.style.format(precision=1)  <--- This causes TypeError: format() got an unexpected keyword argument 'precision'. Any idea why? 

# And then I tried the dictionary approach. No error from this but it does not format numbers
df.style.format({'Margin': '{:.1f}', 'Margin_Rate': '{:.2f}'})

# And then I tried apply background-color and none of them worked..
def highlight_cell(val):
    color = 'yellow' if val == 'Movies' else ''
    return f"bacground-color: {color}"

df.style.applymap(highlight_cell)

# No number formatting nor background color change in the excel sheet...
df.to_excel('retails.xlsx', engine='openpyxl')

他们都不管用..。有什么想法吗?还是我错过了什么?

谢谢。

添加df.to_dict(),正如Kraigolas建议的

代码语言:javascript
复制
{'Category': {0: 'TV', 1: 'Steros', 2: 'Phones', 3: 'Cell Phones', 4: 'Video Consoles', 5: 'Video Games', 6: 'Wearables', 7: 'Tablets', 8: 'Movies', 9: 'Laptops'}, 'Month': {0: 'July', 1: 'August', 2: 'September', 3: 'October', 4: 'November', 5: 'December', 6: 'January', 7: 'February', 8: 'March', 9: 'April'}, 'Sales': {0: 50000, 1: 45000, 2: 22000, 3: 90000, 4: 12000, 5: 7000, 6: 28000, 7: 75000, 8: 25000, 9: 100000}, 'Margin': {0: 10354.64, 1: 39379.38, 2: 8130.23, 3: 8089.23, 4: 8564.69, 5: 3187.74, 6: 8470.51, 7: 8649.36, 8: 20372.73, 9: 62309.34}, 'Margin_Rate': {0: 0.2070928, 1: 0.8750973333333333, 2: 0.3695559090909091, 3: 0.08988033333333333, 4: 0.7137241666666667, 5: 0.45539142857142856, 6: 0.3025182142857143, 7: 0.1153248, 8: 0.8149092, 9: 0.6230934}}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-21 03:14:03

您的代码中有几个问题:

如果没有应用样式,"background"

  • your函数中的
  • 类型应该返回''None,而不是"background-color:"
  • you应该导出df.style.applymap的返回,而不是df

若要设置小数,请在float_format="%0.1f"中使用to_excel

代码语言:javascript
复制
# And then I tried apply background-color and none of them worked..
def highlight_cell(val):
    color = 'yellow' 
    return f"background-color: {color}" if val == 'Movies' else ''

(df.style.applymap(highlight_cell)
   .to_excel('retails.xlsx', engine='openpyxl', float_format="%0.1f")
)

产出:

没有excel导出的格式:

代码语言:javascript
复制
(df.style.applymap(highlight_cell)
   .format({'Margin': '{:.1f}', 'Margin_Rate': '{:.2f}'})
   #.to_excel('retails.xlsx', engine='openpyxl')
)

产出:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73794884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档