A TableView is a list of TableRow objects representing snapshot rows plus a list of SnapshotField objects representing the snapshot fields. You can generate a TableView object from a snapshot through the snapshot’s table_rows attribute or by calling the snapshot’s get_rows( ) method (see Obtaining a TableView from a snapshot below).

TableView attributes

Name Type Description
fields list A list of Field objects.

TableRow objects

Each TableRow object represents one data record, or row, of the parent snapshot. Although you can think of it as a list of field values, TableRow objects let you reference fields by name or dictionary-style, as well as by numeric index. For example, to print the values of a field with name neighborhood:

dataset = public.datasets.get('d8c29d0d-f283-4eb5-b4d4-460c9779d05d')
tableview = dataset.current_snapshot.get_rows()[0:3] # First three rows
for row in tableview:
    print(row.neighborhood)
    print(row['neighborhood'])

Note: Dictionary-style is useful if you don’t know the key name in advance.

Field objects

Each Field object represents one snapshot column, or field. See SnapshotField in the API reference for a list of attributes.

Obtaining a TableView from a snapshot

Method 1

Use this if you specified row_limit > 0 snapshot request. In this case, use the snapshot’s table_rows attribute, for example:

snapshot = public.datasets.get(
    'd8c29d0d-f283-4eb5-b4d4-460c9779d05d',
    query='mexican',
    row_limit=10
).current_snapshot
tableview = snapshot.table_rows

You’ll get back a TableView object with the number of rows you requested (max. 10,000) that match any filter criteria you specified.

Method 2

Use this if you didn’t specify row_limit in the snapshot request, or you want to request new rows. In this case, use the snapshot’s get_rows method, for example:

snapshot = public.datasets.get(
    'd8c29d0d-f283-4eb5-b4d4-460c9779d05d'
).current_snapshot
tableview = snapshot.get_rows(query='mexican', row_limit=10)

By default, you’ll get back a TableView object with the first 200 rows, but you can use row_limit to request a different number (max. 10,000). The get_rows( ) method takes the same arguments as snapshots.get( ).

Creating a DataFrame from a TableView

You can create a pandas DataFrame from a TableView as shown in the example below.

To set the columns names on the DataFrame, you’ll need to pass a list of field names. Since the TableView’s fields attribute returns a list of Field objects, you can’t use it directly. The example shows how to create a list of field names.

import pandas as pd
import enigma

public = enigma.Public()
public.set_auth(apikey='YOUR-API-KEY')

snapshot = public.datasets.get('d8c29d0d-f283-4eb5-b4d4-460c9779d05d').current_snapshot
tableview = snapshot.get_rows(
    query_mode='advanced',
    query='boro:brooklyn'
)
fields = [field.name for field in tableview.fields]
df = pd.DataFrame(tableview, columns=fields)