Skip to content

Commit 798f8e7

Browse files
committed
DOC: add examples to set_index method
1 parent 04356a8 commit 798f8e7

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

pandas/core/frame.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,9 +2947,37 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
29472947
29482948
Examples
29492949
--------
2950-
>>> indexed_df = df.set_index(['A', 'B'])
2951-
>>> indexed_df2 = df.set_index(['A', [0, 1, 2, 0, 1, 2]])
2952-
>>> indexed_df3 = df.set_index([[0, 1, 2, 0, 1, 2]])
2950+
>>> df = pd.DataFrame({'month': [1,4,7,10],
2951+
... 'year': [2012,2014,2013,2014],
2952+
... 'sale':[55, 40, 84, 31]})
2953+
month sale year
2954+
0 1 55 2012
2955+
1 4 40 2014
2956+
2 7 84 2013
2957+
3 10 31 2014
2958+
2959+
2960+
Set the index to become the 'month' column:
2961+
2962+
>>> df.set_index('month')
2963+
sale year
2964+
month
2965+
1 55 2012
2966+
4 40 2014
2967+
7 84 2013
2968+
10 31 2014
2969+
2970+
2971+
Create a multi-index using columns 'year' and 'month':
2972+
2973+
>>> df.set_index(['year', 'month'])
2974+
sale
2975+
year month
2976+
2012 1 55
2977+
2014 4 40
2978+
2013 7 84
2979+
2014 10 31
2980+
29532981
29542982
Returns
29552983
-------

0 commit comments

Comments
 (0)