'How to find the frequency of the most frequent value (mode) of a series in polars?
import polars as pl
df = pl.DataFrame({
"tags": ["a", "a", "a", "b", "c", "c", "c", "c", "d"]
})
This is how to compute the most frequent element of the column using the .mode expression:
df.select([
pl.col("tags").mode().alias("mode"),
])
How can I display also the frequency/count of that mode?
Solution 1:[1]
I found this method, but I'm not sure if there's a better way:
df.select([
pl.col("tags").mode().alias("mode"),
pl.col("tags").filter(pl.col("tags") == pl.col("tags").mode()).count().alias("count")
])
Output:
shape: (1, 2)
????????????????
? mode ? count ?
? --- ? --- ?
? str ? u32 ?
????????????????
? c ? 4 ?
????????????????
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | astrojuanlu |
