pythonpython-polars

how custom sort of rows in polars


How to sort row with spesific order

df = pl.DataFrame({"currency": ["EUR","EUR","EUR","USD","USD","USD"], "alphabet": ["A","B","C","A","B","C"]})

i need to descending the currency and custom sort of alphabet

expected to be like this

shape: (6, 2)
┌──────────┬──────────┐
│ currency ┆ alphabet │
│ ---      ┆ ---      │
│ str      ┆ str      │
╞══════════╪══════════╡
│ USD      ┆ C        │
│ USD      ┆ A        │
│ USD      ┆ B        │
│ EUR      ┆ C        │
│ EUR      ┆ A        │
│ EUR      ┆ B        │
└──────────┴──────────┘

Solution

  • For example you can make your own order of pl.Categorical data using pl.StringCache.

    df = pl.DataFrame({
        "currency": ["EUR","EUR","EUR","USD","USD","USD","USD"],
        "alphabet": ["A","B","C","A","B","C","A"]
    })
    
    with pl.StringCache():
        currency = sorted(["EUR", "USD"], reverse=True)
        pl.Series(["C", "A", "B", *currency]).cast(pl.Categorical)
        
        df = df.with_columns(
            pl.col(pl.String).cast(pl.Categorical),
        ).sort(
            pl.col(pl.Categorical).to_physical()
        )
        
        print(df)
    
    ┌──────────┬──────────┐
    │ currency ┆ alphabet │
    │ ---      ┆ ---      │
    │ cat      ┆ cat      │
    ╞══════════╪══════════╡
    │ USD      ┆ C        │
    │ USD      ┆ A        │
    │ USD      ┆ A        │
    │ USD      ┆ B        │
    │ EUR      ┆ C        │
    │ EUR      ┆ A        │
    │ EUR      ┆ B        │
    └──────────┴──────────┘