We can now apply Python to analyze data. We will work with data stored in Table structures.

Tables are a fundamental way of representing data sets. A table can be viewed in two ways:

  • a sequence of named columns that each describe a single attribute of all entries in a data set, or
  • a sequence of rows that each contain all information about a single individual in a data set. We will study tables in great detail in the next several chapters. For now, we will just introduce a few methods without going into technical details.

The table cones has been imported for us; later we will see how, but here we will just work with it. First, let's take a look at it.

  1. cones
FlavorColorPrice
strawberrypink 3.55
chocolate light brown4.75
chocolate dark brown 5.25
strawberrypink 5.25
chocolate dark brown 5.25
bubblegum pink 4.75

The table has six rows. Each row corresponds to one ice cream cone. The ice cream cones are the individuals.

Each cone has three attributes: flavor, color, and price. Each column contains the data on one of these attributes, and so all the entries of any single column are of the same kind. Each column has a label. We will refer to columns by their labels.

A table method is just like a function, but it must operate on a table. So the call looks like

name_of_table.method(arguments)

For example, if you want to see just the first two rows of a table, you can use the table method show.

  1. cones.show(2)
FlavorColorPrice
strawberrypink 3.55
chocolate light brown4.75

… (4 rows omitted)

You can replace 2 by any number of rows. If you ask for more than six, you will only get six, because cones only has six rows.

Choosing Sets of Columns

The method select creates a new table consisting of only the specified columns.

  1. cones.select('Flavor')
Flavor
strawberry
chocolate
chocolate
strawberry
chocolate
bubblegum

This leaves the original table unchanged.

  1. cones
FlavorColorPrice
strawberrypink 3.55
chocolate light brown4.75
chocolate dark brown 5.25
strawberrypink 5.25
chocolate dark brown 5.25
bubblegum pink 4.75

You can select more than one column, by separating the column labels by commas.

  1. cones.select('Flavor', 'Price')
FlavorPrice
strawberry3.55
chocolate 4.75
chocolate 5.25
strawberry5.25
chocolate 5.25
bubblegum 4.75

You can also drop columns you don't want. The table above can be created by dropping the Color column.

  1. cones.drop('Color')
FlavorPrice
strawberry3.55
chocolate 4.75
chocolate 5.25
strawberry5.25
chocolate 5.25
bubblegum 4.75

You can name this new table and look at it again by just typing its name.

  1. no_colors = cones.drop('Color')
  2.  
  3. no_colors
FlavorPrice
strawberry3.55
chocolate 4.75
chocolate 5.25
strawberry5.25
chocolate 5.25
bubblegum 4.75

Like select, the drop method creates a smaller table and leaves the original table unchanged. In order to explore your data, you can create any number of smaller tables by using choosing or dropping columns. It will do no harm to your original data table.

Sorting Rows

The sort method creates a new table by arranging the rows of the original table in ascending order of the values in the specified column. Here the cones table has been sorted in ascending order of the price of the cones.

  1. cones.sort('Price')
FlavorColorPrice
strawberrypink 3.55
chocolate light brown4.75
bubblegum pink 4.75
chocolate dark brown 5.25
strawberrypink 5.25
chocolate dark brown 5.25

To sort in descending order, you can use an optional argument to sort. As the name implies, optional arguments don't have to be used, but they can be used if you want to change the default behavior of a method.

By default, sort sorts in increasing order of the values in the specified column. To sort in decreasing order, use the optional argument descending=True.

  1. cones.sort('Price', descending=True)
FlavorColorPrice
chocolate dark brown 5.25
strawberrypink 5.25
chocolate dark brown 5.25
bubblegum pink 4.75
chocolate light brown4.75
strawberrypink 3.55

Like select and drop, the sort method leaves the original table unchanged.

Selecting Rows that Satisfy a Condition

The where method creates a new table consisting only of the rows that satisfy a given condition. In this section we will work with a very simple condition, which is that the value in a specified column must be equal to a value that we also specify. Thus the where method has two arguments.

The code in the cell below creates a table consisting only of the rows corresponding to chocolate cones.

  1. cones.where('Flavor', 'chocolate')
FlavorColorPrice
chocolatelight brown4.75
chocolatedark brown 5.25
chocolatedark brown 5.25

The arguments, separated by a comma, are the label of the column and the value we are looking for in that column. The where method can also be used when the condition that the rows must satisfy is more complicated. In those situations the call will be a little more complicated as well.

It is important to provide the value exactly. For example, if we specify Chocolate instead of chocolate, then where correctly finds no rows where the flavor is Chocolate.

  1. cones.where('Flavor', 'Chocolate')
FlavorColorPrice

Like all the other table methods in this section, where leaves the original table unchanged.

Example: Salaries in the NBA

"The NBA is the highest paying professional sports league in the world," reported CNN in March 2016. The table nba contains the salaries of all National Basketball Association players in 2015-2016.

Each row represents one player. The columns are:

Column LabelDescription
PLAYERPlayer's name
POSITIONPlayer's position on team
TEAMTeam name
SALARYPlayer's salary in 2015-2016, in millions of dollars

The code for the positions is PG (Point Guard), SG (Shooting Guard), PF (Power Forward), SF (Small Forward), and C (Center). But what follows doesn't involve details about how basketball is played.

The first row shows that Paul Millsap, Power Forward for the Atlanta Hawks, had a salary of almost $\$18.7$ million in 2015-2016.

  1. nba
PLAYERPOSITIONTEAMSALARY
Paul Millsap PF Atlanta Hawks18.6717
Al Horford C Atlanta Hawks12
Tiago Splitter C Atlanta Hawks9.75625
Jeff Teague PG Atlanta Hawks8
Kyle Korver SG Atlanta Hawks5.74648
Thabo Sefolosha SF Atlanta Hawks4
Mike Scott PF Atlanta Hawks3.33333
Kent Bazemore SF Atlanta Hawks2
Dennis Schroder PG Atlanta Hawks1.7634
Tim Hardaway Jr.SG Atlanta Hawks1.30452

… (407 rows omitted)

Fans of Stephen Curry can find his row by using where.

  1. nba.where('PLAYER', 'Stephen Curry')
PLAYERPOSITIONTEAMSALARY
Stephen CurryPG Golden State Warriors11.3708

We can also create a new table called warriors consisting of just the data for the Golden State Warriors.

  1. warriors = nba.where('TEAM', 'Golden State Warriors')
  2. warriors
PLAYERPOSITIONTEAMSALARY
Klay Thompson SG Golden State Warriors15.501
Draymond Green PF Golden State Warriors14.2609
Andrew Bogut C Golden State Warriors13.8
Andre Iguodala SF Golden State Warriors11.7105
Stephen Curry PG Golden State Warriors11.3708
Jason Thompson PF Golden State Warriors7.00847
Shaun Livingston PG Golden State Warriors5.54373
Harrison Barnes SF Golden State Warriors3.8734
Marreese SpeightsC Golden State Warriors3.815
Leandro Barbosa SG Golden State Warriors2.5

… (4 rows omitted)

By default, the first 10 lines of a table are displayed. You can use show to display more or fewer. To display the entire table, use show with no argument in the parentheses.

  1. warriors.show()
PLAYERPOSITIONTEAMSALARY
Klay Thompson SG Golden State Warriors15.501
Draymond Green PF Golden State Warriors14.2609
Andrew Bogut C Golden State Warriors13.8
Andre Iguodala SF Golden State Warriors11.7105
Stephen Curry PG Golden State Warriors11.3708
Jason Thompson PF Golden State Warriors7.00847
Shaun Livingston PG Golden State Warriors5.54373
Harrison Barnes SF Golden State Warriors3.8734
Marreese SpeightsC Golden State Warriors3.815
Leandro Barbosa SG Golden State Warriors2.5
Festus Ezeli C Golden State Warriors2.00875
Brandon Rush SF Golden State Warriors1.27096
Kevon Looney SF Golden State Warriors1.13196
Anderson Varejao PF Golden State Warriors0.289755

The nba table is sorted in alphabetical order of the team names. To see how the players were paid in 2015-2016, it is useful to sort the data by salary. Remember that by default, the sorting is in increasing order.

  1. nba.sort('SALARY')
PLAYERPOSITIONTEAMSALARY
Thanasis AntetokounmpoSF New York Knicks 0.030888
Jordan McRae SG Phoenix Suns 0.049709
Cory Jefferson PF Phoenix Suns 0.049709
Elliot Williams SG Memphis Grizzlies 0.055722
Orlando Johnson SG Phoenix Suns 0.055722
Phil Pressey PG Phoenix Suns 0.055722
Keith Appling PG Orlando Magic 0.061776
Sean Kilpatrick SG Denver Nuggets 0.099418
Erick Green PG Utah Jazz 0.099418
Jeff Ayres PF Los Angeles Clippers0.111444

… (407 rows omitted)

These figures are somewhat difficult to compare as some of these players changed teams during the season and received salaries from more than one team; only the salary from the last team appears in the table.

The CNN report is about the other end of the salary scale – the players who are among the highest paid in the world. To identify these players we can sort in descending order of salary and look at the top few rows.

  1. nba.sort('SALARY', descending=True)
PLAYERPOSITIONTEAMSALARY
Kobe Bryant SF Los Angeles Lakers 25
Joe Johnson SF Brooklyn Nets 24.8949
LeBron James SF Cleveland Cavaliers 22.9705
Carmelo AnthonySF New York Knicks 22.875
Dwight Howard C Houston Rockets 22.3594
Chris Bosh PF Miami Heat 22.1927
Chris Paul PG Los Angeles Clippers 21.4687
Kevin Durant SF Oklahoma City Thunder20.1586
Derrick Rose PG Chicago Bulls 20.0931
Dwyane Wade SG Miami Heat 20

… (407 rows omitted)

Kobe Bryant, since retired, was the highest earning NBA player in 2015-2016.

This page was created by The Jupyter Book Community