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
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:
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)
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: