Demo of Brunel on Cars Data

The Data

We read the data into a pandas data frame. In this case we are grabbing some data that represents cars. We read it in and call the brunel use method to ensure the names are usable


In [1]:
import pandas as pd
import brunel

cars = pd.read_csv("data/cars.csv")

cars.head(6)


Out[1]:
mpg cylinders engine horsepower weight acceleration year origin name
0 18.0 8 307.0 130.0 3504 12.0 70 American chevrolet chevelle malibu
1 15.0 8 350.0 165.0 3693 11.5 70 American buick skylark 320
2 18.0 8 318.0 150.0 3436 11.0 70 American plymouth satellite
3 16.0 8 304.0 150.0 3433 12.0 70 American amc rebel sst
4 17.0 8 302.0 140.0 3449 10.5 70 American ford torino
5 15.0 8 429.0 198.0 4341 10.0 70 American ford galaxie 500

Basics

We import the Brunel module and create a couple of simple scatterplots. We use the brunel magic to do so

The basic format of each call to Brunel is simple; whether it is a single line or a set of lines (a cell magic), they are concatenated together, and the result interprested as one command.

This command must start with an ACTION, but may have a set of options at the end specified as ACTION :: OPTIONS.

ACTION is the Brunel action string; OPTIONS are key=value pairs:

  • data defines the pandas dataframe to use. If not specified, the pandas data that best fits the action command will be used
  • width and height may be supplied to set the resulting size

For details on the Brunel Action languages, see the Online Docs on Bluemix


In [2]:
%brunel data('cars') x(mpg) y(horsepower) color(origin) filter(horsepower)  :: width=800, height=300


Out[2]:

In [3]:
%brunel bar data('cars') x(origin) y(mpg) mean(mpg) animate(year:6)   :: width=800, height=300


Out[3]:

In [4]:
%brunel data('cars') edge yrange(origin, year) chord size(#count) color(origin) :: width=500, height=400


Out[4]:

In [5]:
%brunel data('cars') treemap x(origin, year, cylinders) color(mpg) mean(mpg) size(#count) label(cylinders) tooltip(#all):: width=900, height=600


Out[5]:

Using the Dataframe

Since Brunel uses the data frame, we can modify or add to that object to show data in different ways. In the following example we apply a function that takes a name and sees if it matches one of a set of sub-strings. We map this function to the car names to create a new column consisting of the names that match either "Ford" or "Buick", and use that in our Brunel action.

Because the Brunel action is long -- we are adding some CSS styling, we split it into two parts for convenience.


In [6]:
def identify(x, search): 
    for y in search: 
        if y.lower() in x.lower(): return y
    return None

cars['Type'] = cars.name.map(lambda x: identify(x, ["Ford", "Buick"]))

In [7]:
%%brunel data('cars') x(engine) y(mpg) color(Type)  style('size:50%; fill:#eee') +
     x(engine) y(mpg) color(Type) text style('text {font-size:14; font-weight:bold; fill:darker}')
    :: width=800, height=800


Out[7]:

In [ ]: