[Python] polars equivalent of pandas set_index() to_dict

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I have a polars dataframe:

    import polars as pl
    df = pl.DataFrame({'index': [1,2,3,2,1],
    'object': [1, 1, 1, 2, 2],
    'period': [1, 2, 4, 4, 23],
    'value': [24, 67, 89, 5, 23]})


    How do I do the following in polars that is easy enough in pandas:

    In [2]: df.to_pandas().groupby("index").last().transpose().to_dict()
    Out[2]:
    {1: {'object': 2, 'period': 23, 'value': 23},
    2: {'object': 2, 'period': 4, 'value': 5},
    3: {'object': 1, 'period': 4, 'value': 89}}

    Continue reading...

Compartilhe esta Página