[Python] Formatting pandas tables that contain lists of floats

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    How can I format numbers in a pandas table, when a cell displays a list of floating point numbers instead of a single float value?

    Here's a code example:

    import pandas as pd
    df = pd.DataFrame(data={'Values':[[0.1231245678,0,0],[1e-10,0,0]]})
    df


    I would like to format the numbers in the table as %.2f. So the table data should be displayed as

    [0.12, 0.00, 0.00]
    [0.00, 0.00, 0.00]


    The usual options:

    pd.set_option('float_format','{:20,.2f}'.format)
    pd.set_option('display.chop_threshold', 0.001)


    only work when table cells contain single numbers.

    Continue reading...

Compartilhe esta Página