turicreate.SFrame.topk

SFrame.topk(self, column_name, k=10, reverse=False)

Get top k rows according to the given column. Result is according to and sorted by column_name in the given order (default is descending). When k is small, topk is more efficient than sort.

Parameters:
column_name : string

The column to sort on

k : int, optional

The number of rows to return

reverse : bool, optional

If True, return the top k rows in ascending order, otherwise, in descending order.

Returns:
out : SFrame

an SFrame containing the top k rows sorted by column_name.

See also

sort

Examples

>>> sf = turicreate.SFrame({'id': range(1000)})
>>> sf['value'] = -sf['id']
>>> sf.topk('id', k=3)
+--------+--------+
|   id   |  value |
+--------+--------+
|   999  |  -999  |
|   998  |  -998  |
|   997  |  -997  |
+--------+--------+
[3 rows x 2 columns]
>>> sf.topk('value', k=3)
+--------+--------+
|   id   |  value |
+--------+--------+
|   1    |  -1    |
|   2    |  -2    |
|   3    |  -3    |
+--------+--------+
[3 rows x 2 columns]