Skip to content
Advertisement

How can you get rolling value count (frequency) with Pandas? (computationally efficient, no loops)

I have a list of values and I want to get their rolling frequency, so something like this:

JavaScript

Of course I can do this with a loop but with a lot of data it can be computationally expensive so I’d much rather use a built-in or something vectorized, etc. But unfortunately, from my searching, there doesn’t seem to be a solution.

Thanks in advance!

Advertisement

Answer

The first n-1 elements of the result of a rolling function with window size n must be NaN per definition.

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement