显示 None 和 pandas 个字典的值 Python

Displaying None and Values with pandas dictionaries Python

None 个值表示下面的字典 a 中的 Last Month 行没有值。我如何才能修改 pandas 样式格式,以便它可以打印 table a 并仍然在设置的列前面放置美元符号?

import numpy as np
import pandas as pd

a =   {'Timeframes': ['Entirety:',
      'Last Month:',
      'Three Months:',
      'Six Months:',
      'Last Year:',
      'Last Two Years:'],
     'Compounding With Lev':  np.array([2398012.89, None, 90.07, 85.29,
            620.39, 30611.48], dtype=object),
     'Compounding With Seperate Levs': np.array([21165662669.71, None, 91.18, 107.54,
            3004.87, 13287947.75], dtype=object),
     'Adjusted Long Compounding Lev': np.array([3.25, None, 1.0, 1.0, 3.5, 4.75], dtype=object),
     'Adjusted Short Compounding Lev': np.array([3.75, None, 1.0, 3.0, 1.0, 2.0], dtype=object),
     'Non Compounding With Lev': np.array([3626.41, None, 89.95, 95.73,
            1577.75, 1380.80], dtype=object),
     'Non Compounding With Seperate Levs': np.array([5679.53, None, 91.15, 408.40,
            1953.53, 2530.58], dtype=object),
     'Adjusted Long NonCompounding Lev': np.array([4.25, None, 1.0, 1.0, 10.5, 4.25], dtype=object),
     'Adjusted Short NonCompounding Lev': np.array([7.75, None, 1.0, 33.25, 1.0, 7.75], dtype=object)}

display(pd.DataFrame(a).style.format(formatter={'Compounding With Lev': '${:,.2f}',
                       'Compounding With Seperate Levs': '${:,.2f}',
                       'Non Compounding With Lev': '${:,.2f}',
                       'Non Compounding With Seperate Levs': '${:,.2f}'}))

预期输出:

import numpy as np
import pandas as pd

a =   {'Timeframes': ['Entirety:',
      'Last Month:',
      'Three Months:',
      'Six Months:',
      'Last Year:',
      'Last Two Years:'],
     'Compounding With Lev':  np.array([2398012.89, None, 90.07, 85.29,
            620.39, 30611.48], dtype=float),
     'Compounding With Seperate Levs': np.array([21165662669.71, None, 91.18, 107.54,
            3004.87, 13287947.75], dtype=float),
     'Adjusted Long Compounding Lev': np.array([3.25, None, 1.0, 1.0, 3.5, 4.75], dtype=float),
     'Adjusted Short Compounding Lev': np.array([3.75, None, 1.0, 3.0, 1.0, 2.0], dtype=float),
     'Non Compounding With Lev': np.array([3626.41, None, 89.95, 95.73,
            1577.75, 1380.80], dtype=float),
     'Non Compounding With Seperate Levs': np.array([5679.53, None, 91.15, 408.40,
            1953.53, 2530.58], dtype=float),
     'Adjusted Long NonCompounding Lev': np.array([4.25, None, 1.0, 1.0, 10.5, 4.25], dtype=float),
     'Adjusted Short NonCompounding Lev': np.array([7.75, None, 1.0, 33.25, 1.0, 7.75], dtype=float)}

display(pd.DataFrame(a).style.format(formatter={'Compounding With Lev': '${:,.2f}',
                       'Compounding With Seperate Levs': '${:,.2f}',
                       'Non Compounding With Lev': '${:,.2f}',
                       'Non Compounding With Seperate Levs': '${:,.2f}'}, na_rep='None'))

通过更改 dtype=float,我们允许 numpynan 值放入数组中。 na_rep 参数是格式化程序在格式化不适用时将放入的内容。

转换字典 a 以防你的字典像从文件中读取的那样并且数组已经是 dtype=object:

for k, v in a.items():
    if isinstance(v, np.ndarray):
        a[k] = v.astype(float)