Avoiding for loops in Pandas

Screenshot 2018-12-30 at 14.05.23.png

There will be times when you are tempted to loop through rows or columns in Pandas to achieve your results – and the lesson I keep learning is Don’t do it!

Every time I’m tempted to write a for loop with Pandas data I find myself clock watching and cursing… 9 times out of 10 there is another way, and here are 2 of my favourite recipes, enjoy :).

Some sample data

Screenshot 2018-12-30 at 14.28.13


The power of groupby() and lambda

Let’s say that for each Type in our dataframe, we want to create a sequenced list of Events. Nothing easier:

Screenshot 2018-12-30 at 14.40.28

The power of itertools and lambda

And now let’s say we’re satisfied with our lists, but we’d like to de-dupe where adjacent events occur (e.g. “two, two” above should be reduced to “two)

Screenshot 2018-12-30 at 14.51.32

And let’s not forget the magic of list comprehension

Let’s say we want to add a “U-” prefix to each of our Deduped_Events:

Screenshot 2018-12-30 at 14.59.48

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: