Indexing & Slicing

Columns

DTable columns can be accessed with DTable.columns

>> iris_dt.columns
Id    SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
1     5.1              3.5             1.4              0.2             Iris-setosa
2     4.9              3.0             1.4              0.2             Iris-setosa
3     4.7              3.2             1.3              0.2             Iris-setosa
4     4.6              3.1             1.5              0.2             Iris-setosa
... 148 more entries ...
<DTable.0x23423a>

The reason iris_dt.columns looks exactly the same is because they are the same (in a way). Accessing .columns returns another DTable with the same columns as original DTable. This would be more obvious when you look at it for slicing. This is how you can access the data of a specific column:

>> iris_dt.columns['SepalLengthCm']
SepalLengthCm
5.1
4.9
4.7
4.6
... 148 more entries ...
<DTable.0x097adfa>

iris_dt.columns['SepalLengthCm'] creates a columnar (aka vertical) slice of the DTable and pulls out one column. The result is another DTable with a single column SepalLengthCm.

The columnar slicing also takes array of column names. Let's look at an example:

>> iris_dt.columns[ ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm'] ]
SepalLengthCm    SepalWidthCm    PetalLengthCm
5.1              3.5             1.4
4.9              3.0             1.4
4.7              3.2             1.3
4.6              3.1             1.5
... 148 more entries ...
<DTable.0x097adfa>

Rows

You can do row-wise (aka horizontal slicing) by using the .rows accessor.

>> iris_dt.rows
Id    SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
1     5.1              3.5             1.4              0.2             Iris-setosa
2     4.9              3.0             1.4              0.2             Iris-setosa
3     4.7              3.2             1.3              0.2             Iris-setosa
4     4.6              3.1             1.5              0.2             Iris-setosa
... 148 more entries ...
<DTable.0x23423a>

Just accessing the .rows gives you all the rows in a new DTable. However, you can slice the rows of a DTable just like you can slice any Python sequence:

>> iris_dt.rows[1]
Id    SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
2     4.9              3.0             1.4              0.2             Iris-setosa
>> iris_dt.rows[1:3]
Id    SepalLengthCm    SepalWidthCm    PetalLengthCm    PetalWidthCm    Species
2     4.9              3.0             1.4              0.2             Iris-setosa
3     4.7              3.2             1.3              0.2             Iris-setosa
4     4.6              3.1             1.5              0.2             Iris-setosa

Remember, slicing by rows would also give you a DTable.

Columns & Rows

You can slice using columns and rows together:

>> iris_dt.columns[ ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm'] ].rows[1:3]
SepalLengthCm    SepalWidthCm    PetalLengthCm
4.9              3.0             1.4
4.7              3.2             1.3
4.6              3.1             1.5

Since, each slice is a DTable by itself, it can be further sliced using the .columns or .rows accessors.

results matching ""

    No results matching ""