@@ -3924,22 +3924,36 @@ def shift(self, periods=1, freq=None, axis=0):
3924
3924
def set_index (self , keys , drop = True , append = False , inplace = False ,
3925
3925
verify_integrity = False ):
3926
3926
"""
3927
+ Set the DataFrame index using existing columns.
3928
+
3927
3929
Set the DataFrame index (row labels) using one or more existing
3928
- columns. By default yields a new object .
3930
+ columns. The index can replace the existing index or expand on it .
3929
3931
3930
3932
Parameters
3931
3933
----------
3932
- keys : column label or list of column labels / arrays
3933
- drop : boolean, default True
3934
- Delete columns to be used as the new index
3935
- append : boolean, default False
3936
- Whether to append columns to existing index
3937
- inplace : boolean, default False
3938
- Modify the DataFrame in place (do not create a new object)
3939
- verify_integrity : boolean, default False
3934
+ keys : label or list of label
3935
+ Name or names of the columns that will be used as the index.
3936
+ drop : bool, default True
3937
+ Delete columns to be used as the new index.
3938
+ append : bool, default False
3939
+ Whether to append columns to existing index.
3940
+ inplace : bool, default False
3941
+ Modify the DataFrame in place (do not create a new object).
3942
+ verify_integrity : bool, default False
3940
3943
Check the new index for duplicates. Otherwise defer the check until
3941
3944
necessary. Setting to False will improve the performance of this
3942
- method
3945
+ method.
3946
+
3947
+ Returns
3948
+ -------
3949
+ DataFrame
3950
+ Changed row labels.
3951
+
3952
+ See Also
3953
+ --------
3954
+ DataFrame.reset_index : Opposite of set_index.
3955
+ DataFrame.reindex : Change to new indices or expand indices.
3956
+ DataFrame.reindex_like : Change to same indices as other DataFrame.
3943
3957
3944
3958
Returns
3945
3959
-------
@@ -3949,22 +3963,23 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
3949
3963
--------
3950
3964
>>> df = pd.DataFrame({'month': [1, 4, 7, 10],
3951
3965
... 'year': [2012, 2014, 2013, 2014],
3952
- ... 'sale':[55, 40, 84, 31]})
3953
- month sale year
3954
- 0 1 55 2012
3955
- 1 4 40 2014
3956
- 2 7 84 2013
3957
- 3 10 31 2014
3966
+ ... 'sale': [55, 40, 84, 31]})
3967
+ >>> df
3968
+ month year sale
3969
+ 0 1 2012 55
3970
+ 1 4 2014 40
3971
+ 2 7 2013 84
3972
+ 3 10 2014 31
3958
3973
3959
3974
Set the index to become the 'month' column:
3960
3975
3961
3976
>>> df.set_index('month')
3962
- sale year
3977
+ year sale
3963
3978
month
3964
- 1 55 2012
3965
- 4 40 2014
3966
- 7 84 2013
3967
- 10 31 2014
3979
+ 1 2012 55
3980
+ 4 2014 40
3981
+ 7 2013 84
3982
+ 10 2014 31
3968
3983
3969
3984
Create a multi-index using columns 'year' and 'month':
3970
3985
@@ -4072,22 +4087,22 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
4072
4087
def reset_index (self , level = None , drop = False , inplace = False , col_level = 0 ,
4073
4088
col_fill = '' ):
4074
4089
"""
4075
- For DataFrame with multi-level index, return new DataFrame with
4076
- labeling information in the columns under the index names, defaulting
4077
- to 'level_0', 'level_1', etc. if any are None. For a standard index,
4078
- the index name will be used (if set), otherwise a default 'index' or
4079
- 'level_0' (if 'index' is already taken) will be used .
4090
+ Reset the index, or a level of it.
4091
+
4092
+ Reset the index of the DataFrame, and use the default one instead.
4093
+ If the DataFrame has a MultiIndex, this method can remove one or more
4094
+ levels .
4080
4095
4081
4096
Parameters
4082
4097
----------
4083
4098
level : int, str, tuple, or list, default None
4084
4099
Only remove the given levels from the index. Removes all levels by
4085
- default
4086
- drop : boolean , default False
4100
+ default.
4101
+ drop : bool , default False
4087
4102
Do not try to insert index into dataframe columns. This resets
4088
4103
the index to the default integer index.
4089
- inplace : boolean , default False
4090
- Modify the DataFrame in place (do not create a new object)
4104
+ inplace : bool , default False
4105
+ Modify the DataFrame in place (do not create a new object).
4091
4106
col_level : int or str, default 0
4092
4107
If the columns have multiple levels, determines which level the
4093
4108
labels are inserted into. By default it is inserted into the first
@@ -4098,13 +4113,20 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
4098
4113
4099
4114
Returns
4100
4115
-------
4101
- resetted : DataFrame
4116
+ DataFrame
4117
+ DataFrame with the new index.
4118
+
4119
+ See Also
4120
+ --------
4121
+ DataFrame.set_index : Opposite of reset_index.
4122
+ DataFrame.reindex : Change to new indices or expand indices.
4123
+ DataFrame.reindex_like : Change to same indices as other DataFrame.
4102
4124
4103
4125
Examples
4104
4126
--------
4105
- >>> df = pd.DataFrame([('bird', 389.0),
4106
- ... ('bird', 24.0),
4107
- ... ('mammal', 80.5),
4127
+ >>> df = pd.DataFrame([('bird', 389.0),
4128
+ ... ('bird', 24.0),
4129
+ ... ('mammal', 80.5),
4108
4130
... ('mammal', np.nan)],
4109
4131
... index=['falcon', 'parrot', 'lion', 'monkey'],
4110
4132
... columns=('class', 'max_speed'))
0 commit comments