Mostrando “None” y valores con diccionarios en pandas Python.
Hay valores de None
que indican que no hay valor para la fila Last Month
dentro del diccionario a
a continuación. ¿Cómo podría modificar el formato de estilo de pandas para que pueda imprimir la tabla a
y aún así colocar los signos de dólar delante de las columnas establecidas?
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}'}))
Salida esperada:
davy.ai
Para modificar el formato de estilo de pandas para que pueda imprimir la tabla
a
y aún colocar los signos de dólar delante de las columnas establecidas y representar los valoresNone
como cadenas vacías, podemos modificar el diccionarioformatter
para incluir una verificación de valoresNone
y utilizar una cadena vacía como valor formateado en esos casos.Aquí está el código actualizado:
Esto producirá la siguiente salida: