Search code examples
python-polars

In Polars how can I display a single row from a dataframe vertically like a pandas series?


I have a polars dataframe with many columns. I want to look at all the data from a single row aligned vertically so that I can see the values in many different columns without it going off the edge of the screen. How can I do this?

E.g. define a dataframe

df = pl.DataFrame({'a':[0,1],'b':[2,3]})

Print df[0] in ipython/jupyter and I get:

Output from a single row of the dataframe

But if I convert df to pandas and print df.iloc[0] I get:

Output from pandas

The latter is very handy when you've got many columns.

I've tried things like df[0].to_series(), but it only prints the first element, not the first row.

My suspicion is that there isn't a direct replacement because the pandas method relies on the series having an index. I think the polars solution will be more like making a two column dataframe where one column is the column names and the other is a value. I'm not sure if there's a method to do that though.

Thanks for any help you can offer!


Solution

  • You can try using unpivot. For example:

    df = pl.DataFrame(
        [
            pl.Series(name="col_str", values=["string1", "string2"]),
            pl.Series(name="col_bool", values=[False, True]),
            pl.Series(name="col_int", values=[1, 2]),
            pl.Series(name="col_float", values=[10.0, 20.0]),
            *[pl.Series(name=f"col_other_{idx}", values=[idx] * 2)
              for idx in range(1, 25)],
        ]
    )
    print(df)
    
    shape: (2, 28)
    ┌─────────┬──────────┬─────────┬───────────┬───┬──────────────┬──────────────┬──────────────┬──────────────┐
    │ col_str ┆ col_bool ┆ col_int ┆ col_float ┆ … ┆ col_other_21 ┆ col_other_22 ┆ col_other_23 ┆ col_other_24 │
    │ ---     ┆ ---      ┆ ---     ┆ ---       ┆   ┆ ---          ┆ ---          ┆ ---          ┆ ---          │
    │ str     ┆ bool     ┆ i64     ┆ f64       ┆   ┆ i64          ┆ i64          ┆ i64          ┆ i64          │
    ╞═════════╪══════════╪═════════╪═══════════╪═══╪══════════════╪══════════════╪══════════════╪══════════════╡
    │ string1 ┆ false    ┆ 1       ┆ 10.0      ┆ … ┆ 21           ┆ 22           ┆ 23           ┆ 24           │
    │ string2 ┆ true     ┆ 2       ┆ 20.0      ┆ … ┆ 21           ┆ 22           ┆ 23           ┆ 24           │
    └─────────┴──────────┴─────────┴───────────┴───┴──────────────┴──────────────┴──────────────┴──────────────┘
    

    To print the first row:

    pl.Config.set_tbl_rows(100)
    df[0].unpivot()
    
    ┌──────────────┬─────────┐
    │ variable     ┆ value   │
    │ ---          ┆ ---     │
    │ str          ┆ str     │
    ╞══════════════╪═════════╡
    │ col_str      ┆ string1 │
    │ col_bool     ┆ false   │
    │ col_int      ┆ 1       │
    │ col_float    ┆ 10.0    │
    │ col_other_1  ┆ 1       │
    │ col_other_2  ┆ 2       │
    │ col_other_3  ┆ 3       │
    │ col_other_4  ┆ 4       │
    │ col_other_5  ┆ 5       │
    │ col_other_6  ┆ 6       │
    │ col_other_7  ┆ 7       │
    │ col_other_8  ┆ 8       │
    │ col_other_9  ┆ 9       │
    │ col_other_10 ┆ 10      │
    │ col_other_11 ┆ 11      │
    │ col_other_12 ┆ 12      │
    │ col_other_13 ┆ 13      │
    │ col_other_14 ┆ 14      │
    │ col_other_15 ┆ 15      │
    │ col_other_16 ┆ 16      │
    │ col_other_17 ┆ 17      │
    │ col_other_18 ┆ 18      │
    │ col_other_19 ┆ 19      │
    │ col_other_20 ┆ 20      │
    │ col_other_21 ┆ 21      │
    │ col_other_22 ┆ 22      │
    │ col_other_23 ┆ 23      │
    │ col_other_24 ┆ 24      │
    └──────────────┴─────────┘
    

    If needed, set the polars.Config.set_tbl_rows option to the number of rows you find acceptable. (This only needs to be done once per session, not every time you print.)

    Notice that all values have been cast to super-type str. (One caution: this approach won't work if any of your columns are of dtype list.)