diff options
| author | rabinnankhwa <rabin.nankhwa@gmail.com> | 2014-08-30 23:52:35 +0545 |
|---|---|---|
| committer | rabinnankhwa <rabin.nankhwa@gmail.com> | 2014-08-30 23:52:35 +0545 |
| commit | f187cef5f45e8e4e59d15b732c6c8307142bb634 (patch) | |
| tree | d2e46c34f5cea3b661eebe838c26c833543af8e6 /tablib/core.py | |
| parent | 87892d7266d8808374d2c32becc9df4687431227 (diff) | |
| download | tablib-f187cef5f45e8e4e59d15b732c6c8307142bb634.tar.gz | |
adding support for creating subset of a dataset.
Diffstat (limited to 'tablib/core.py')
| -rw-r--r-- | tablib/core.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tablib/core.py b/tablib/core.py index 76f4569..3fc55cb 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -933,6 +933,42 @@ class Dataset(object): self.__headers = None + def subset(self, rows=None, cols=None): + """Returns a new instance of the :class:`Dataset`, + including only specified rows and columns. + """ + + # Don't return if no data + if not self: + return + + if rows is None: + rows = list(range(self.height)) + + if cols is None: + cols = list(self.headers) + + _dset = Dataset() + + #filtering rows and columns + _dset.headers = list(cols) + + _dset._data = [] + for row_no, row in enumerate(self._data): + data_row = [] + for key in _dset.headers: + if key in self.headers: + pos = self.headers.index(key) + data_row.append(row[pos]) + else: + raise KeyError + + if row_no in rows: + _dset.append(row=Row(data_row)) + + return _dset + + class Databook(object): """A book of :class:`Dataset` objects. |
