Skip to content

Instantly share code, notes, and snippets.

@nowox
Last active March 28, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nowox/1d0165221ac17a6de071f51c5428b599 to your computer and use it in GitHub Desktop.
Save nowox/1d0165221ac17a6de071f51c5428b599 to your computer and use it in GitHub Desktop.
Example with Pandas
In [119]: d = {
...: 'test1': {
...: 0: {'a': {'min': 1, 'mid': 12, 'max': 13},
...: 'b': {'min': 2, 'mid': 22, 'max': 23}},
...: 3: {'a': {'min': 3, 'mid': 32, 'max': 33},
...: 'b': {'min': 4, 'mid': 42, 'max': 43}},
...: 8: {'a': {'min': 5, 'mid': 52, 'max': 53},
...: 'b': {'min': 6, 'mid': 62, 'max': 63}},
...: },
...: 'test2': {
...: 0: {'a': {'min': 9, 'mid': 12, 'max': 13},
...: 'b': {'min': 8, 'mid': 22, 'max': 23}},
...: 3: {'a': {'min': 7, 'mid': 32, 'max': 33},
...: 'b': {'min': 6, 'mid': 42, 'max': 43}},
...: 8: {'a': {'min': 5, 'mid': 52, 'max': 53},
...: 'b': {'min': 4, 'mid': 62, 'max': 63}},
...: },
...: }
In [120]: (pd.DataFrame.from_dict(d, orient="index")
...: .stack().apply(pd.Series)
...: .stack().apply(pd.Series)
...: .rename_axis(("Tests", "Id", "Item")))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-120-8cfba8c10c3d> in <module>()
2 .stack().apply(pd.Series)
3 .stack().apply(pd.Series)
----> 4 .rename_axis(("Tests", "Id", "Item")))
/usr/lib/python2.7/site-packages/pandas/core/generic.pyc in rename_axis(self, mapper, axis, copy, inplace)
618 d = {'copy': copy, 'inplace': inplace}
619 d[axis] = mapper
--> 620 return self.rename(**d)
621
622 #----------------------------------------------------------------------
/usr/lib/python2.7/site-packages/pandas/core/frame.pyc in rename(self, index, columns, **kwargs)
2522 def rename(self, index=None, columns=None, **kwargs):
2523 return super(DataFrame, self).rename(index=index, columns=columns,
-> 2524 **kwargs)
2525
2526 @Appender(_shared_docs['fillna'] % _shared_doc_kwargs)
/usr/lib/python2.7/site-packages/pandas/core/generic.pyc in rename(self, *args, **kwargs)
587
588 baxis = self._get_block_manager_axis(axis)
--> 589 result._data = result._data.rename_axis(f, axis=baxis, copy=copy)
590 result._clear_item_cache()
591
/usr/lib/python2.7/site-packages/pandas/core/internals.pyc in rename_axis(self, mapper, axis, copy)
2233 """
2234 obj = self.copy(deep=copy)
-> 2235 obj.set_axis(axis, _transform_index(self.axes[axis], mapper))
2236 return obj
2237
/usr/lib/python2.7/site-packages/pandas/core/internals.pyc in _transform_index(index, func)
3993 """
3994 if isinstance(index, MultiIndex):
-> 3995 items = [tuple(func(y) for y in x) for x in index]
3996 return MultiIndex.from_tuples(items, names=index.names)
3997 else:
/usr/lib/python2.7/site-packages/pandas/core/internals.pyc in <genexpr>((y,))
3993 """
3994 if isinstance(index, MultiIndex):
-> 3995 items = [tuple(func(y) for y in x) for x in index]
3996 return MultiIndex.from_tuples(items, names=index.names)
3997 else:
TypeError: 'tuple' object is not callable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment