summaryrefslogtreecommitdiff
path: root/tablib/core.py
diff options
context:
space:
mode:
authorLuca Beltrame <einar@heavensinferno.net>2010-11-11 09:00:06 +0100
committerLuca Beltrame <einar@heavensinferno.net>2010-11-11 09:00:06 +0100
commit657ab98d04d7d32b36eeae63c4da48e6b0a9c100 (patch)
treef49ddf32323478b232a4ad31fb12e8fa878218dc /tablib/core.py
parent2bb052599031f883ec220921c45d23eee6ae32f3 (diff)
downloadtablib-657ab98d04d7d32b36eeae63c4da48e6b0a9c100.tar.gz
Support for Dataset transposition. Unit-tested.
Diffstat (limited to 'tablib/core.py')
-rw-r--r--tablib/core.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/tablib/core.py b/tablib/core.py
index 1417000..23026a2 100644
--- a/tablib/core.py
+++ b/tablib/core.py
@@ -507,7 +507,35 @@ class Dataset(object):
_dset._data = [row for row in _dset._data if row.has_tag(tag)]
return _dset
-
+
+ def transpose(self):
+ """Transpose a :class:`Dataset`, turning rows into columns and vice
+ versa, returning a new ``Dataset`` instance. The first row of the
+ original instance becomes the new header row."""
+
+ # Don't transpose if there is no data
+ if not self:
+ return
+
+ _dset = Dataset()
+ # The first element of the headers stays in the headers,
+ # it is our "hinge" on which we rotate the data
+ new_headers = [self.headers[0]] + self[self.headers[0]]
+
+ _dset.headers = new_headers
+ for column in self.headers:
+
+ if column == self.headers[0]:
+ # It's in the headers, so skip it
+ continue
+
+ # Adding the column name as now they're a regular column
+ row_data = [column] + self[column]
+ row_data = Row(row_data)
+ _dset.append(row=row_data)
+
+ return _dset
+
def wipe(self):
"""Removes all content and headers from the :class:`Dataset` object."""
self._data = list()