@@ -3060,57 +3060,67 @@ class max_speed
3060
3060
... ('mammal', 'monkey')],
3061
3061
... names=['class', 'name'])
3062
3062
>>> columns = pd.MultiIndex.from_tuples([('speed', 'max'),
3063
- ... ('speed ', 'type')])
3063
+ ... ('species ', 'type')])
3064
3064
>>> df = pd.DataFrame([(389.0, 'fly'),
3065
3065
... ( 24.0, 'fly'),
3066
3066
... ( 80.5, 'run'),
3067
3067
... (np.nan, 'jump')],
3068
3068
... index=index,
3069
3069
... columns=columns)
3070
3070
>>> df
3071
- speed
3072
- max type
3071
+ speed species
3072
+ max type
3073
3073
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
3078
3078
3079
3079
If the index has multiple levels, we can reset a subset of them:
3080
3080
3081
3081
>>> df.reset_index(level='class')
3082
- class speed
3083
- max type
3082
+ class speed species
3083
+ max type
3084
3084
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
3089
3089
3090
3090
If we are not dropping the index, by default, it is placed in the top
3091
3091
level. We can place it in another level:
3092
3092
3093
3093
>>> df.reset_index(level='class', col_level=1)
3094
- speed
3095
- class max type
3094
+ speed species
3095
+ class max type
3096
3096
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
3101
3101
3102
3102
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`:
3105
3104
3106
3105
>>> 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
3109
3108
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
3114
3124
"""
3115
3125
inplace = validate_bool_kwarg (inplace , 'inplace' )
3116
3126
if inplace :
0 commit comments