[Python] Simplest way to convert aggregated data to visualize in polars

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

  1. Stack

    Stack Membro Participativo

    Suppose I have aggregated the mean and the median of some value over 3 months, like:

    df = (data.group_by('month_code').agg(pl.col('value').mean().alias('avg'),
    pd.col('value').median().alias('med')
    )
    .sort('month_code')
    .collect()
    )


    Resulting in something like:

    df = pd.DataFrame({'month': ['M202412','M202501','M202502'],
    'avg': [0.037824, 0.03616, 0.038919],
    'med': [0.01381, 0.013028, 0.014843]
    })


    And I'd like to visualize it, so should convert to the format:

    df_ = pd.DataFrame({'month': ['M202412','M202501','M202502']*2,
    'type': ['avg','avg','avg','med','med','med'],
    'value': [0.037824, 0.03616, 0.038919, 0.01381, 0.013028, 0.014843],
    })


    Which is then easy to visualize:

    df_.plot.line(x='month',y='value',color='type').properties(width=400, height=350, title='avg and med')


    What is the simplest way to convert df to df_ above?

    Continue reading...

Compartilhe esta Página