Does polars have the function to encode string column into integers (1, 2, 3) like pandas.factorize?
Didn’t find it in the polars documentation
>Solution :
Perhaps you’re looking for a dense rank or the categorical type.
df = pl.DataFrame({"column": ["foo", "bar", "baz", "foo", "foo"]})
df.with_columns(rank = pl.col("column").rank("dense"))
shape: (5, 2)
┌────────┬──────┐
│ column | rank │
│ --- | --- │
│ str | u32 │
╞════════╪══════╡
│ foo | 3 │
│ bar | 1 │
│ baz | 2 │
│ foo | 3 │
│ foo | 3 │
└────────┴──────┘