python - How to deal with Pandas groupby choosing a different index type based on the data? -


i've written pandas code want when there data. when there no data, code errors out. turns out groupby() creating different index type based on data.

here code:

>>> import pandas pd >>> cols = ['a', 'b', 'c'] >>> >>> = pd.dataframe(data=[[1,2,3]], columns=cols) >>> b = pd.dataframe(data=[], columns=cols) >>> >>> a.groupby(['a','b'])['c'].sum().index multiindex(levels=[[1], [2]],            labels=[[0], [0]],            names=['a', 'b']) >>> a.groupby(['a','b'])['c'].sum().index.get_level_values('a') int64index([1], dtype='int64', name='a') >>> >>> b.groupby(['a','b'])['c'].sum().index index([], dtype='object') >>> b.groupby(['a','b'])['c'].sum().index.get_level_values('a') traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "/cv/.virtualenvs/ts/lib/python3.5/site-packages/pandas/indexes/base.py", line 2214, in get_level_values     self._validate_index_level(level)   file "/cv/.virtualenvs/ts/lib/python3.5/site-packages/pandas/indexes/base.py", line 1325, in _validate_index_level     (level, self.name)) keyerror: 'level must same name (none)' 

questions:

  • what rules pandas uses choose type of index?
  • i'd expect pandas create multiindex no data, since explicitly groupby() several columns; why not creating multiindex?
  • my code relying on groupby() operation resulting in multiindex; should avoid doing this? if so, recommended approach?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -