1 of 14

Lecture 6

Rows

DATA 8

Summer 2017

Slides created by John DeNero (denero@berkeley.edu), Ani Adhikari (adhikari@berkeley.edu), and Sam Lau (samlau95@berkeley.edu).

2 of 14

Tables Review

3 of 14

Table Structure

  • A Table is a sequence of labeled columns
  • Labels are strings
  • Columns are arrays, all with the same length

Name

Code

Area (m2)

California

CA

163696

Nevada

NV

110567

Label

Column

Row

4 of 14

Table Methods

  • Creating and extending tables:
    • Table().with_columns and Table.read_table
  • Finding the size: num_rows and num_columns
  • Referring to columns: labels, relabeling, and indices
    • labels and relabeled; column indices start at 0
  • Accessing data in a column
    • column takes a label or index and returns an array
  • Using array methods to work with data in columns
    • item, sum, min, max, and so on
  • Creating new tables containing some of the original columns:
    • select, drop

5 of 14

Quick Check

The table students has columns Name, ID, and Score.

Write one line of code that evaluates to:

  1. A table consisting of only the column labeled Name

students.select('Name')

b) The largest score

students.column('Score').max()

students.select(0)

max(students.column('Score'))

6 of 14

Sort

7 of 14

Sorting Tables

Tables are ordered collections of rows

The sort method creates a new table with the same rows in a different order (the original table is unaffected)

The show method displays the first rows of a table

(Demo)

8 of 14

Lists

9 of 14

Lists are Generic Sequences

A list is a sequence of values (just like an array), but the values can all have different types

[2+3, 'four', Table().with_column('K', [3, 4])]

If you create a table column from a list, it will be converted to an array automatically

(Demo)

10 of 14

Take

11 of 14

Take Rows, Select Columns

The select method returns a table with only some columns

The take method returns a table with only some rows

  • Rows are numbered, starting at 0
  • Taking a single number returns a one-row table
  • Taking a list of numbers returns a table as well

(Demo)

12 of 14

Where

13 of 14

The Where Method

The where method specifies a column and a condition

It returns a new table with all rows satisfying the condition

(Demo)

14 of 14

Manipulating Rows

  • t.sort(column) sorts the rows in increasing order
  • t.take(row_numbers) keeps the numbered rows
    • Each row has an index, starting at 0
  • t.where(column, are.condition) keeps all rows for which a column's value satisfies a condition
  • t.where(column, value) keeps all rows containing a certain value in a column