def set_index(self, keys, drop=True, append=False, inplace=False,<EOL>verify_integrity=False):
inplace = validate_bool_kwarg(inplace, '<STR_LIT>')<EOL>if not isinstance(keys, list):<EOL><INDENT>keys = [keys]<EOL><DEDENT>err_msg = ('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>missing = []<EOL>for col in keys:<EOL><INDENT>if isinstance(col, (ABCIndexClass, ABCSeries, np.ndarray,<EOL>list, abc.Iterator)):<EOL><INDENT>if getattr(col, '<STR_LIT>', <NUM_LIT:1>) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError(err_msg)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>found = col in self.columns<EOL><DEDENT>except TypeError:<EOL><INDENT>raise TypeError(err_msg + '<STR_LIT>'<EOL>'<STR_LIT>'.format(type(col)))<EOL><DEDENT>else:<EOL><INDENT>if not found:<EOL><INDENT>missing.append(col)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if missing:<EOL><INDENT>raise KeyError('<STR_LIT>'.format(missing))<EOL><DEDENT>if inplace:<EOL><INDENT>frame = self<EOL><DEDENT>else:<EOL><INDENT>frame = self.copy()<EOL><DEDENT>arrays = []<EOL>names = []<EOL>if append:<EOL><INDENT>names = [x for x in self.index.names]<EOL>if isinstance(self.index, ABCMultiIndex):<EOL><INDENT>for i in range(self.index.nlevels):<EOL><INDENT>arrays.append(self.index._get_level_values(i))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>arrays.append(self.index)<EOL><DEDENT><DEDENT>to_remove = []<EOL>for col in keys:<EOL><INDENT>if isinstance(col, ABCMultiIndex):<EOL><INDENT>for n in range(col.nlevels):<EOL><INDENT>arrays.append(col._get_level_values(n))<EOL><DEDENT>names.extend(col.names)<EOL><DEDENT>elif isinstance(col, (ABCIndexClass, ABCSeries)):<EOL><INDENT>arrays.append(col)<EOL>names.append(col.name)<EOL><DEDENT>elif isinstance(col, (list, np.ndarray)):<EOL><INDENT>arrays.append(col)<EOL>names.append(None)<EOL><DEDENT>elif isinstance(col, abc.Iterator):<EOL><INDENT>arrays.append(list(col))<EOL>names.append(None)<EOL><DEDENT>else:<EOL><INDENT>arrays.append(frame[col]._values)<EOL>names.append(col)<EOL>if drop:<EOL><INDENT>to_remove.append(col)<EOL><DEDENT><DEDENT>if len(arrays[-<NUM_LIT:1>]) != len(self):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(<EOL>len_self=len(self),<EOL>len_col=len(arrays[-<NUM_LIT:1>])<EOL>))<EOL><DEDENT><DEDENT>index = ensure_index_from_sequences(arrays, names)<EOL>if verify_integrity and not index.is_unique:<EOL><INDENT>duplicates = index[index.duplicated()].unique()<EOL>raise ValueError('<STR_LIT>'.format(<EOL>dup=duplicates))<EOL><DEDENT>for c in set(to_remove):<EOL><INDENT>del frame[c]<EOL><DEDENT>index._cleanup()<EOL>frame.index = index<EOL>if not inplace:<EOL><INDENT>return frame<EOL><DEDENT>
Set the DataFrame index using existing columns.

Set the DataFrame index (row labels) using one or more existing
columns or arrays (of the correct length). The index can replace the
existing index or expand on it.

Parameters
----------
keys : label or array-like or list of labels/arrays
    This parameter can be either a single column key, a single array of
    the same length as the calling DataFrame, or a list containing an
    arbitrary combination of column keys and arrays. Here, "array"
    encompasses :class:`Series`, :class:`Index`, ``np.ndarray``, and
    instances of :class:`~collections.abc.Iterator`.
drop : bool, default True
    Delete columns to be used as the new index.
append : bool, default False
    Whether to append columns to existing index.
inplace : bool, default False
    Modify the DataFrame in place (do not create a new object).
verify_integrity : bool, default False
    Check the new index for duplicates. Otherwise defer the check until
    necessary. Setting to False will improve the performance of this
    method.

Returns
-------
DataFrame
    Changed row labels.

See Also
--------
DataFrame.reset_index : Opposite of set_index.
DataFrame.reindex : Change to new indices or expand indices.
DataFrame.reindex_like : Change to same indices as other DataFrame.

Examples
--------
>>> df = pd.DataFrame({'month': [1, 4, 7, 10],
...                    'year': [2012, 2014, 2013, 2014],
...                    'sale': [55, 40, 84, 31]})
>>> df
   month  year  sale
0      1  2012    55
1      4  2014    40
2      7  2013    84
3     10  2014    31

Set the index to become the 'month' column:

>>> df.set_index('month')
       year  sale
month
1      2012    55
4      2014    40
7      2013    84
10     2014    31

Create a MultiIndex using columns 'year' and 'month':

>>> df.set_index(['year', 'month'])
            sale
year  month
2012  1     55
2014  4     40
2013  7     84
2014  10    31

Create a MultiIndex using an Index and a column:

>>> df.set_index([pd.Index([1, 2, 3, 4]), 'year'])
         month  sale
   year
1  2012  1      55
2  2014  4      40
3  2013  7      84
4  2014  10     31

Create a MultiIndex using two Series:

>>> s = pd.Series([1, 2, 3, 4])
>>> df.set_index([s, s**2])
      month  year  sale
1 1       1  2012    55
2 4       4  2014    40
3 9       7  2013    84
4 16     10  2014    31