Python List Comprehension with Plotly

This blog will assume you have the following skill-levels:

Python: Medium

Plotly: Basic

I always seem to be finding bizarre corners of Python that stretch my intellectual abilities way past their natural sense of safety.

Hot Chick Thinking About List Comprehension

This time, I’m going to attempt to explain TWO concepts mashed into one very complicated (but powerful) tool.  Amazingly, I couldn't find this approach in any tutorial online.  Maybe I didn't look hard enough, but either way, here's my attempt.

First let’s have a quick introduction to Python List Comprehension.  The name itself is daunting, but the concept itself is less so.

List Comprehension is meant to replace a for-loop when creating new lists.  For example, a regular, straightforward way to break up a string into it’s constituent characters might go something like this:

myLetters = []
for letter in ‘giraffe’:
    myLetters.append(letter)
print(myLetters)

This would create a new list (myLetters) containing this:

['g','i','r','a','f','f','e']

List Comprehension can do this in a slightly ‘simpler’ way.  Example:

myLetters = [ letter for letter in ‘giraffe’]
print( myLetters )
['g','i','r','a','f','f','e']

It is philosophically similar to Python’s lamda operator.  You don’t HAVE to use it, but it can come in very handy. And here’s an example of when that’s true.

Plotly.  It’s amazing, but it can also be bloody difficult to maneuver.  Especially when you’re as stupid as me. And by the very fact that you’re reading MY blog, I have to assume you are too.

We’re going to assume that I’m analyzing a set of data that contains a series of temperature measurements.  The set includes the day of the week that each temperature was taken as well as the time of that day.

We want to plot the data with 7 different lines, each one representing the day of the week.  The x axis will be TIME, and the y axis will be TEMPERATURE.

Each day is represented many times in the data set, so we really want to split the data up by DAYS so that we can draw each line appropriately.

data = [{
'x': df['TIME'],
'y': df[df['DAY']==day]['AVERAGE_TEMP']
} for day in df['DAY'].unique()]

Let's rip apart this bizarre tiny thing and make some sense of it...

First off, let's break out the List Comprehension and ignore the guts for now

data = [day for day in df['DAY'].unique()]

In or example, this will simply create a list of all the unique day-names:

['TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY', 'MONDAY']

Now for the middle stuff.  I haven't specified WHAT is in these data objects, but it doesn't matter in order to get the concept:

'x': df['TIME'], 
'y': df[df['DAY']==day]['AVERAGE_TEMP']

This is essentially the first 'day' value in the List Comprehension.  For each unique 'day' value, we're going to make an x point for the times represented, and a y point for each of the temperature values.  In the above example you'll get something like the following.  Note, we get 7 coloured lines, each one representing a day in the week:

 

Leave a Reply

Your email address will not be published. Required fields are marked *