Skip to content

Commit 9026a79

Browse files
committed
DOC: added docs for MultiIndex.set_levels (pandas-dev#28294)
shortened lines to pass checks
1 parent 9f03837 commit 9026a79

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/source/user_guide/advanced.rst

+31
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,37 @@ completely analogous way to selecting a column in a regular DataFrame:
177177
See :ref:`Cross-section with hierarchical index <advanced.xs>` for how to select
178178
on a deeper level.
179179

180+
.. _advanced.set_levels:
181+
182+
Set values in levels
183+
~~~~~~~~~~~~~~~~~~~~~
184+
185+
The method :meth:`~MultiIndex.set_levels` changes the ``levels`` attribute by
186+
passing a new value for each index in the level. It is assumed that a new value
187+
is provided for each code describing values in the level.
188+
For example:
189+
190+
.. ipython:: python
191+
192+
df.columns # original MultiIndex columns
193+
194+
df.columns.levels # original MultiIndex column levels
195+
196+
df.columns.set_levels([1, 3, 5, 7], level=0)
197+
198+
df.columns.set_levels([1, 3, 5, 7], level=0).levels
199+
200+
If you pass more values than the number of index values in the level,
201+
``set_levels`` will still pass the values to the level. The passed values
202+
are stored in the MultiIndex ``FrozenList`` even though the index values
203+
may be truncated in the MultiIndex output from ``set_levels``.
204+
205+
.. ipython:: python
206+
207+
df.columns.set_levels([1, 3, 5, 7], level=1)
208+
209+
df.columns.set_levels([1, 3, 5, 7], level=1).levels
210+
180211
.. _advanced.shown_levels:
181212

182213
Defined levels

0 commit comments

Comments
 (0)