[Python] Creating Python Polars DataFrame/LazyFrame from Dict representing single Frame row?

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a Polars DataFrame or LazyFrame (I'm not sure what is appropriate) from a list of dictionaries where each of the dicts represents one row of data. Each individual dict has fields of various data types and most importantly some fields have array-like data. The array-like data related to on key can vary in length from dict to dict. A single row dictionary looks like this:

    {
    'col1': int,
    'col2': int,
    'col3': str,
    ...
    'colA': str:timestamp,
    'colB': int(long),
    'colC': float,
    ...
    'colK': bool,
    ...
    'colL': str[L], <-- length differs from dict to dict
    'colM': float[M], <-- length differs from dict to dict
    'colN': int[N], <-- length differs from dict to dict
    ...
    }


    Currently, I am trying to do the following:

    for row_dict in list_of_row_dicts:
    dataframe = pd.DataFrame(row_dict)
    slice_dataframe = slice_dataframe.join(other=dataframe)


    This fails with the error message:


    polars.exceptions.ShapeError: could not create a new DataFrame: height of column 'colL' (x) does not match height of column 'colM' (y)
    ​

    How can I achieve what I am trying to do?

    Continue reading...

Compartilhe esta Página