Pandas Get the Most Frequent Values of a Column

pandas rolling get most frequent value in window

Use rolling().apply with mode method:

df.rolling(window = 3).apply(lambda x: x.mode()[0])

x will be a pd.Series object of length equal to window. mode method will return a list of most frequent values. lambda will return the first item from the list of modes.

How to find the frequency of the most frequent value (mode) of a series in polars?

There is a value_counts expression. This expression will return a Struct datatype where the first field is the unique value and the second field is the count of that value.

df.select([
pl.col("tags").value_counts()
])
shape: (4, 1)
┌───────────┐
│ tags │
│ --- │
│ struct[2] │
╞═══════════╡
│ {"c",4} │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"a",3} │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"b",1} │
├╌╌╌╌╌╌╌╌╌╌╌┤
│ {"d",1} │
└───────────┘

Or if you want to have that result as a DataFrame:

(df.select([
pl.col("tags").value_counts()
]).to_series().struct.to_frame())
shape: (4, 2)
┌──────┬────────┐
│ tags ┆ counts │
│ --- ┆ --- │
│ str ┆ u32 │
╞══════╪════════╡
│ c ┆ 4 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ a ┆ 3 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ d ┆ 1 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ b ┆ 1 │
└──────┴────────┘

Edited:
Which can be even simpler:

df["tags"].value_counts()


Related Topics



Leave a reply



Submit