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

Question:

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?

Asked By: Frank Jimenez

||

Answers:

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   │
└─────┴──────┴──────────┘
Answered By: ignoring_gravity
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.