Python Polars: how to convert a list of dictionaries to polars dataframe without using pandas

I have a list of dictionaries like this:

[{"id": 1, "name": "Joe", "lastname": "Bloggs"}, {"id": 2, "name": "Bob", "lastname": "Wilson"}]

And I would like to transform it to a polars dataframe. I’ve tried going via pandas but if possible, I’d like to avoid using pandas.

Any thoughts?

>Solution :

Just pass it to pl.DataFrame

In [2]: pl.DataFrame([{"id": 1, "name": "Joe", "lastname": "Bloggs"}, {"id": 2, "name": "Bob", "lastname": "Wilson"}])
Out[2]:
shape: (2, 3)
┌─────┬──────┬──────────┐
│ id  ┆ name ┆ lastname │
│ --- ┆ ---  ┆ ---      │
│ i64 ┆ str  ┆ str      │
╞═════╪══════╪══════════╡
│ 1   ┆ Joe  ┆ Bloggs   │
│ 2   ┆ Bob  ┆ Wilson   │
└─────┴──────┴──────────┘

Leave a Reply