@@ -3023,35 +3023,98 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
3023
3023
3024
3024
Examples
3025
3025
--------
3026
- >>> df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]},
3027
- ... index=pd.Index(['a', 'b', 'c', 'd'],
3028
- ... name='idx'))
3026
+ >>> df = pd.DataFrame([('bird', 389.0),
3027
+ ... ('bird', 24.0),
3028
+ ... ('mammal', 80.5),
3029
+ ... ('mammal', np.nan)],
3030
+ ... index=['falcon', 'parrot', 'lion', 'monkey'],
3031
+ ... columns=('class', 'max_speed'))
3032
+ >>> df
3033
+ class max_speed
3034
+ falcon bird 389.0
3035
+ parrot bird 24.0
3036
+ lion mammal 80.5
3037
+ monkey mammal NaN
3038
+
3039
+ When we reset the index, the old index is added as a column, and a
3040
+ new sequential index is used:
3041
+
3029
3042
>>> df.reset_index()
3030
- idx a b
3031
- 0 a 1 5
3032
- 1 b 2 6
3033
- 2 c 3 7
3034
- 3 d 4 8
3035
-
3036
- >>> arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo',
3037
- ... 'foo', 'qux', 'qux']),
3038
- ... np.array(['one', 'two', 'one', 'two', 'one', 'two',
3039
- ... 'one', 'two'])]
3040
- >>> df2 = pd.DataFrame(
3041
- ... np.random.randn(8, 4),
3042
- ... index=pd.MultiIndex.from_arrays(arrays,
3043
- ... names=['a', 'b']))
3044
- >>> df2.reset_index(level='a')
3045
- a 0 1 2 3
3046
- b
3047
- one bar -1.099413 0.291838 0.598198 0.162181
3048
- two bar -0.312184 -0.119904 0.250360 0.364378
3049
- one baz 0.713596 -0.490636 0.074967 -0.297857
3050
- two baz 0.998397 0.524499 -2.228976 0.901155
3051
- one foo 0.923204 0.920695 1.264488 1.476921
3052
- two foo -1.566922 0.783278 -0.073656 0.266027
3053
- one qux -0.230470 0.109800 -1.383409 0.048421
3054
- two qux -0.865993 -0.865984 0.705367 -0.170446
3043
+ index class max_speed
3044
+ 0 falcon bird 389.0
3045
+ 1 parrot bird 24.0
3046
+ 2 lion mammal 80.5
3047
+ 3 monkey mammal NaN
3048
+
3049
+ We can use the `drop` parameter to avoid the old index being added as
3050
+ a column:
3051
+
3052
+ >>> df.reset_index(drop=True)
3053
+ class max_speed
3054
+ 0 bird 389.0
3055
+ 1 bird 24.0
3056
+ 2 mammal 80.5
3057
+ 3 mammal NaN
3058
+
3059
+ You can also use `reset_index` with `MultiIndex`.
3060
+
3061
+ >>> index = pd.MultiIndex.from_tuples([('bird', 'falcon'),
3062
+ ... ('bird', 'parrot'),
3063
+ ... ('mammal', 'lion'),
3064
+ ... ('mammal', 'monkey')],
3065
+ ... names=['class', 'name'])
3066
+ >>> columns = pd.MultiIndex.from_tuples([('speed', 'max'),
3067
+ ... ('speed', 'type')])
3068
+ >>> df = pd.DataFrame([(389.0, 'fly'),
3069
+ ... ( 24.0, 'fly'),
3070
+ ... ( 80.5, 'run'),
3071
+ ... (np.nan, 'jump')],
3072
+ ... index=index,
3073
+ ... columns=columns)
3074
+ >>> df
3075
+ speed
3076
+ max type
3077
+ class name
3078
+ bird falcon 389.0 fly
3079
+ parrot 24.0 fly
3080
+ mammal lion 80.5 run
3081
+ monkey NaN jump
3082
+
3083
+ If the index has multiple levels, we can reset a subset of them:
3084
+
3085
+ >>> df.reset_index(level='class')
3086
+ class speed
3087
+ max type
3088
+ name
3089
+ falcon bird 389.0 fly
3090
+ parrot bird 24.0 fly
3091
+ lion mammal 80.5 run
3092
+ monkey mammal NaN jump
3093
+
3094
+ If we are not dropping the index, by default, it is placed in the top
3095
+ level. We can place it in another level:
3096
+
3097
+ >>> df.reset_index(level='class', col_level=1)
3098
+ speed
3099
+ class max type
3100
+ name
3101
+ falcon bird 389.0 fly
3102
+ parrot bird 24.0 fly
3103
+ lion mammal 80.5 run
3104
+ monkey mammal NaN jump
3105
+
3106
+ When the index is inserted under another level, we can specify under
3107
+ which one with the parameter `col_fill`. If we specify a nonexistent
3108
+ level, it is created:
3109
+
3110
+ >>> df.reset_index(level='class', col_level=1, col_fill='species')
3111
+ species speed
3112
+ class max type
3113
+ name
3114
+ falcon bird 389.0 fly
3115
+ parrot bird 24.0 fly
3116
+ lion mammal 80.5 run
3117
+ monkey mammal NaN jump
3055
3118
"""
3056
3119
inplace = validate_bool_kwarg (inplace , 'inplace' )
3057
3120
if inplace :
0 commit comments