File tree 1 file changed +29
-1
lines changed
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:
157
157
index.get_level_values(0 )
158
158
index.get_level_values(' second' )
159
159
160
-
161
160
Basic indexing on axis with MultiIndex
162
161
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163
162
@@ -176,6 +175,35 @@ completely analogous way to selecting a column in a regular DataFrame:
176
175
See :ref: `Cross-section with hierarchical index <advanced.xs >` for how to select
177
176
on a deeper level.
178
177
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)
179
207
180
208
Data alignment and using ``reindex ``
181
209
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can’t perform that action at this time.
0 commit comments