Pandas Window Functions — ELI5

Imagine you’re tracking how many steps you walk each day. Looking at just today’s number doesn’t tell you much — maybe you walked a lot today because you went hiking, or barely any because you were sick.

But if you look at the last 7 days together, you get a much better picture. That’s a window — a sliding frame that moves through your data and looks at a chunk at a time instead of one point at a time.

Think of it like a magnifying glass that slides across a ruler. At each position, the magnifying glass shows you a few numbers at once. You can ask questions about what’s inside the glass: “What’s the average of these numbers?” or “What’s the biggest one?”

Then you slide the glass one position forward and ask the same questions again. The glass always shows the same number of items, but different items each time.

This is incredibly useful for spotting trends. If your step count today is 8,000, is that good or bad? If the 7-day average is 5,000, then today is great. If the 7-day average is 12,000, today is a low day. The window gives you context that a single number can’t.

Pandas has two main flavors: rolling (fixed number of recent rows) and expanding (everything from the start up to now). Rolling is like looking at the last week. Expanding is like looking at your entire history so far.

One thing to remember: Window functions don’t shrink your data like averages normally do. You get one answer for every row — each answer just considers the nearby rows for context.

pythonpandasdata-science

See Also