pythondictionarypython-polars

How to use to_dict and orient='records' in Polars that is being used in pandas?


Using polars, I am not getting the same output as pandas when calling to_dict.

Pandas.

df = pd.DataFrame({
    'column_1': [1, 2, 1, 4, 5],
    'column_2': ['Alice', 'Bob', 'Alice', 'Tom', 'Tom'],
    'column_3': ['Alice1', 'Bob', 'Alice2', 'Tom', 'Tom']
})

df.to_dict(orient='records')

produces

[{'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice1'}, {'column_1': 2, 'column_2': 'Bob', 'column_3': 'Bob'}, {'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice2'}, {'column_1': 4, 'column_2': 'Tom', 'column_3': 'Tom'}, {'column_1': 5, 'column_2': 'Tom', 'column_3': 'Tom'}]

Polars.

df = pl.DataFrame({
    'column_1': [1, 2, 1, 4, 5],
    'column_2': ['Alice', 'Bob', 'Alice', 'Tom', 'Tom'],
    'column_3': ['Alice1', 'Bob', 'Alice2', 'Tom', 'Tom']
})

df.to_dict(as_series=False)

produces

{'column_1': [1, 2, 1, 4, 5], 'column_2': ['Alice', 'Bob', 'Alice', 'Tom', 'Tom'], 'column_3': ['Alice1', 'Bob', 'Alice2', 'Tom', 'Tom']}

Here, the first example is pandas and the output I got when using to_dict with orient='records'. I expected to have the same output in polars.

How can I replicate pandas' behaviour in polars?


Solution

  • To replicate the behaviour of pandas' to_dict with orient="records", you can use pl.DataFrame.to_dicts (notice the extra s).

    df.to_dicts()
    

    Output.

    [{'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice1'},
     {'column_1': 2, 'column_2': 'Bob', 'column_3': 'Bob'},
     {'column_1': 1, 'column_2': 'Alice', 'column_3': 'Alice2'},
     {'column_1': 4, 'column_2': 'Tom', 'column_3': 'Tom'},
     {'column_1': 5, 'column_2': 'Tom', 'column_3': 'Tom'}]