Skip to content

Commit 4ce5340

Browse files
gfyoungjreback
authored andcommitted
DOC: Add more examples for reset_index (#17055)
1 parent a7eb1a7 commit 4ce5340

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

pandas/core/frame.py

+37-27
Original file line numberDiff line numberDiff line change
@@ -3060,57 +3060,67 @@ class max_speed
30603060
... ('mammal', 'monkey')],
30613061
... names=['class', 'name'])
30623062
>>> columns = pd.MultiIndex.from_tuples([('speed', 'max'),
3063-
... ('speed', 'type')])
3063+
... ('species', 'type')])
30643064
>>> df = pd.DataFrame([(389.0, 'fly'),
30653065
... ( 24.0, 'fly'),
30663066
... ( 80.5, 'run'),
30673067
... (np.nan, 'jump')],
30683068
... index=index,
30693069
... columns=columns)
30703070
>>> df
3071-
speed
3072-
max type
3071+
speed species
3072+
max type
30733073
class name
3074-
bird falcon 389.0 fly
3075-
parrot 24.0 fly
3076-
mammal lion 80.5 run
3077-
monkey NaN jump
3074+
bird falcon 389.0 fly
3075+
parrot 24.0 fly
3076+
mammal lion 80.5 run
3077+
monkey NaN jump
30783078
30793079
If the index has multiple levels, we can reset a subset of them:
30803080
30813081
>>> df.reset_index(level='class')
3082-
class speed
3083-
max type
3082+
class speed species
3083+
max type
30843084
name
3085-
falcon bird 389.0 fly
3086-
parrot bird 24.0 fly
3087-
lion mammal 80.5 run
3088-
monkey mammal NaN jump
3085+
falcon bird 389.0 fly
3086+
parrot bird 24.0 fly
3087+
lion mammal 80.5 run
3088+
monkey mammal NaN jump
30893089
30903090
If we are not dropping the index, by default, it is placed in the top
30913091
level. We can place it in another level:
30923092
30933093
>>> df.reset_index(level='class', col_level=1)
3094-
speed
3095-
class max type
3094+
speed species
3095+
class max type
30963096
name
3097-
falcon bird 389.0 fly
3098-
parrot bird 24.0 fly
3099-
lion mammal 80.5 run
3100-
monkey mammal NaN jump
3097+
falcon bird 389.0 fly
3098+
parrot bird 24.0 fly
3099+
lion mammal 80.5 run
3100+
monkey mammal NaN jump
31013101
31023102
When the index is inserted under another level, we can specify under
3103-
which one with the parameter `col_fill`. If we specify a nonexistent
3104-
level, it is created:
3103+
which one with the parameter `col_fill`:
31053104
31063105
>>> df.reset_index(level='class', col_level=1, col_fill='species')
3107-
species speed
3108-
class max type
3106+
species speed species
3107+
class max type
31093108
name
3110-
falcon bird 389.0 fly
3111-
parrot bird 24.0 fly
3112-
lion mammal 80.5 run
3113-
monkey mammal NaN jump
3109+
falcon bird 389.0 fly
3110+
parrot bird 24.0 fly
3111+
lion mammal 80.5 run
3112+
monkey mammal NaN jump
3113+
3114+
If we specify a nonexistent level for `col_fill`, it is created:
3115+
3116+
>>> df.reset_index(level='class', col_level=1, col_fill='genus')
3117+
genus speed species
3118+
class max type
3119+
name
3120+
falcon bird 389.0 fly
3121+
parrot bird 24.0 fly
3122+
lion mammal 80.5 run
3123+
monkey mammal NaN jump
31143124
"""
31153125
inplace = validate_bool_kwarg(inplace, 'inplace')
31163126
if inplace:

0 commit comments

Comments
 (0)