dataframelistpython-polars

Create Polars Dataframe From List of Lists


I would like to take a list of lists where the first row are column names and load it into a Polars Dataframe in one line so I get maximum performance.

In Pandas, I would do this:

table = [['Heading1', 'Heading2'], [1 , 2], [3, 4]]

df = pd.DataFrame(table[1:], columns=table[0])

But in Polars its not clear. I would like to avoid having to serialize the data first (eg to use read_csv).


Solution

  • You can do the same, but columns= is replaced with schema=

    pl.DataFrame(table[1:], schema=table[0])
    
    shape: (2, 2)
    ┌──────────┬──────────┐
    │ Heading1 ┆ Heading2 │
    │ ---      ┆ ---      │
    │ i64      ┆ i64      │
    ╞══════════╪══════════╡
    │ 1        ┆ 3        │
    │ 2        ┆ 4        │
    └──────────┴──────────┘
    

    Polars attempts to infer the orientation from the data, so you may want to explictly set it to row in this case.

    pl.DataFrame(table[1:], schema=table[0], orient="row")
    
    shape: (2, 2)
    ┌──────────┬──────────┐
    │ Heading1 ┆ Heading2 │
    │ ---      ┆ ---      │
    │ i64      ┆ i64      │
    ╞══════════╪══════════╡
    │ 1        ┆ 2        │
    │ 3        ┆ 4        │
    └──────────┴──────────┘