Pythonic Lambda Queries


There is not much literature out there praising the usefulness of lambda functions, so I thought I would make this post about pythonic lambda functions.

Lambda functions originate in lambda calculus as abstractions and application of functions using variable binding and substitution. Such functions take nested form to encapsulate non-specific code with out cluttering code. Because lambda functions are not be bound to any identifier they are also less error-prone.

Given a large data set with the following structure, where dashes represent some data entry such as a string, int or datetime data value, this is often useful in building complex queries of big data.


Screenshot (69)

The code below instantiates lambda functions that will select sub columns from a sequential level column for each given category.

# levels of each subtasks
tutorial= lambda x: x.loc[x['Level']=='1 Tutorial']
training= lambda x: x.loc[x['Level']=='2 Training']
distractors= lambda x: x.loc[x['Level']=='3 Distractors']

Here the dataframe is returned to as a queryable data structure, and is reused for different variety of skills.

snack=df.loc[df['Skill'] == "Snack"]
peeling_bannanas= dat.loc[dat['Subtask']=='Peeling Bannanas']

The above statement would return all the data from the table, selecting “Snack” skill as the basis column and all the tutorial levels from “peeling bananas” subskill. As such these little functions can be efficient components, critical to complex dependency heavy programming.


If you like these blog posts or want to comment and or share something do so below and follow py-guy!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s