Skip to content

Commit 1212fe0

Browse files
aernlundjreback
authored andcommitted
DOC: Reset index examples
closes #16416 Author: aernlund <awe220@nyumc.org> Closes #16967 from aernlund/reset_index_docs and squashes the following commits: 3c6a4b6 [aernlund] DOC: added examples to reset_index 4838155 [aernlund] DOC: added examples to reset_index 2a51e2b [aernlund] DOC: added examples to reset_index
1 parent 4f04d0b commit 1212fe0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pandas/core/frame.py

+32
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,38 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
30203020
Returns
30213021
-------
30223022
resetted : DataFrame
3023+
3024+
Examples
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'))
3029+
>>> 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
30233055
"""
30243056
inplace = validate_bool_kwarg(inplace, 'inplace')
30253057
if inplace:

pandas/core/series.py

+31
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,37 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
948948
Returns
949949
----------
950950
resetted : DataFrame, or Series if drop == True
951+
952+
Examples
953+
--------
954+
>>> s = pd.Series([1, 2, 3, 4], index=pd.Index(['a', 'b', 'c', 'd'],
955+
... name = 'idx'))
956+
>>> s.reset_index()
957+
index 0
958+
0 0 1
959+
1 1 2
960+
2 2 3
961+
3 3 4
962+
963+
>>> arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo',
964+
... 'foo', 'qux', 'qux']),
965+
... np.array(['one', 'two', 'one', 'two', 'one', 'two',
966+
... 'one', 'two'])]
967+
>>> s2 = pd.Series(
968+
... np.random.randn(8),
969+
... index=pd.MultiIndex.from_arrays(arrays,
970+
... names=['a', 'b']))
971+
>>> s2.reset_index(level='a')
972+
a 0
973+
b
974+
one bar -0.286320
975+
two bar -0.587934
976+
one baz 0.710491
977+
two baz -1.429006
978+
one foo 0.790700
979+
two foo 0.824863
980+
one qux -0.718963
981+
two qux -0.055028
951982
"""
952983
inplace = validate_bool_kwarg(inplace, 'inplace')
953984
if drop:

0 commit comments

Comments
 (0)