File tree Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Expand file tree Collapse file tree 1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -157,7 +157,6 @@ location at a particular level:
157157 index.get_level_values(0 )
158158 index.get_level_values(' second' )
159159
160-
161160 Basic indexing on axis with MultiIndex
162161~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163162
@@ -176,6 +175,35 @@ completely analogous way to selecting a column in a regular DataFrame:
176175 See :ref: `Cross-section with hierarchical index <advanced.xs >` for how to select
177176on a deeper level.
178177
178+ .. note ::
179+
180+ The repr of a ``MultiIndex `` shows ALL the defined levels of an index, even
181+ if the they are not actually used. When slicing an index, you may notice this.
182+ For example:
183+
184+ .. ipython :: python
185+
186+ # original multi-index
187+ df.columns
188+
189+ # sliced
190+ df[[' foo' ,' qux' ]].columns
191+
192+ This is done to avoid a recomputation of the levels in order to make slicing
193+ highly performant. If you want to see the actual used levels.
194+
195+ .. ipython :: python
196+
197+ df[[' foo' ,' qux' ]].columns.values
198+
199+ # for a specific level
200+ df[[' foo' ,' qux' ]].columns.get_level_values(0 )
201+
202+ To reconstruct the multiindex with only the used levels
203+
204+ .. ipython :: python
205+
206+ pd.MultiIndex.from_tuples(df[[' foo' ,' qux' ]].columns.values)
179207
180208 Data alignment and using ``reindex ``
181209~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can’t perform that action at this time.
0 commit comments