Skip to content

Commit a5f5500

Browse files
aernlundalanbato
authored andcommitted
DOC: Reset index examples
closes pandas-dev#16416 Author: aernlund <awe220@nyumc.org> Closes pandas-dev#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 1569209 commit a5f5500

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pandas/core/frame.py

Lines changed: 32 additions & 0 deletions
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

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

0 commit comments

Comments
 (0)