es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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:

imagen

Tags:  , , , ,

Answer

  1. Avatar for 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 valores None como cadenas vacías, podemos modificar el diccionario formatter para incluir una verificación de valores None y utilizar una cadena vacía como valor formateado en esos casos.

    Aquí está el código actualizado:

    import numpy as np
    import pandas as pd
    
    def format_value(val):
        if val is None:
            return ''
        return f'${val:,.2f}'
    
    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': format_value,
        'Compounding With Seperate Levs': format_value,
        'Non Compounding With Lev': format_value,
        'Non Compounding With Seperate Levs': format_value,
    }))
    

    Esto producirá la siguiente salida:

    enter image description here

Comments are closed.