【问题标题】:why style.format from pandas doesn't work in spyder?为什么 pandas 的 style.format 在 spyder 中不起作用?
【发布时间】:2020-09-01 21:18:23
【问题描述】:

也许我错过了什么,我有这个:

import pandas as pd

series1 = pd.Series({'DEPOSITOS': 353254})
series2 = pd.Series({'DEPOSITOS': 54464.43})
series3 = pd.Series({'COLOCACIONES': 6381763761})
series4 = pd.Series({'COLOCACIONES': 687513761})
series5 = pd.Series({'%PROV': 0.95})
series6 = pd.Series({'%PROV': 0.25})

top100v3 = pd.DataFrame([series1, series2, series3,series4, series5, series6])

top100v3.style.format({"DEPOSITOS": "{:,.0f}"})

但是当我运行它时它不会改变数据框(top100v3),它只是返回:

C:\Users\yvs\Anaconda3\lib\site-packages\pandas\core\ops\array_ops.py:253:FutureWarning:元素比较失败;而是返回标量,但将来将执行元素比较 res_values = 方法(右值) 出[1]:

【问题讨论】:

  • 它是否可以在另一个编码环境(例如 jupyter notebook)中工作?你的熊猫版本是什么?你能给我们一个可重现的例子吗?
  • 是的,它适用于 jupyter notebook,pandas 1.0.3

标签: python pandas spyder


【解决方案1】:

显然 style.format 使用 html 和 css 支持,而 spyder 不支持,但 jupyer notebook 支持,所以如果我想继续使用 spyder,我必须更改将格式应用于列的方式

#for one column
top100v3["DEPOSITOS"] = top100v3["DEPOSITOS"].apply(lambda x : "{:,.0f}".format(x))

#for many columns  
col_formatlist = ["DEPOSITOS", "COLOCACIONES"]

for col in col_formatlist:
top100v3[col] = top100v3[col].apply(lambda x: f'{x:,.0f}'.format(x))

top100v3["%PROV"] = top100v3["%PROV"].apply(lambda x: "{:.2%}".format(x))

更多格式请查看https://mkaz.blog/code/python-string-format-cookbook/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-30
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2021-07-28
    • 2019-07-31
    • 2020-07-19
    相关资源
    最近更新 更多