Skip to content

Commit cfcf07b

Browse files
geoninjaTomAugspurger
authored andcommitted
DOC: Added set_index examples (#16467)
1 parent 96f3e7c commit cfcf07b

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

pandas/core/frame.py

+38-3
Original file line numberDiff line numberDiff line change
@@ -2947,9 +2947,44 @@ 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+
Set the index to become the 'month' column:
2960+
2961+
>>> df.set_index('month')
2962+
sale year
2963+
month
2964+
1 55 2012
2965+
4 40 2014
2966+
7 84 2013
2967+
10 31 2014
2968+
2969+
Create a multi-index using columns 'year' and 'month':
2970+
2971+
>>> df.set_index(['year', 'month'])
2972+
sale
2973+
year month
2974+
2012 1 55
2975+
2014 4 40
2976+
2013 7 84
2977+
2014 10 31
2978+
2979+
Create a multi-index using a set of values and a column:
2980+
2981+
>>> df.set_index([[1, 2, 3, 4], 'year'])
2982+
month sale
2983+
year
2984+
1 2012 1 55
2985+
2 2014 4 40
2986+
3 2013 7 84
2987+
4 2014 10 31
29532988
29542989
Returns
29552990
-------

0 commit comments

Comments
 (0)