Skip to content

Commit fa65ba5

Browse files
committed
Updated set_index parameter append along with an example
1 parent 7281a18 commit fa65ba5

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pandas/core/frame.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5868,10 +5868,6 @@ def set_index(
58685868
columns or arrays (of the correct length). The index can replace the
58695869
existing index or expand on it.
58705870
5871-
.. warning::
5872-
Setting a new index will remove the current index column,
5873-
unless you first call `reset_index`.
5874-
58755871
Parameters
58765872
----------
58775873
keys : label or array-like or list of labels/arrays
@@ -5884,6 +5880,8 @@ def set_index(
58845880
Delete columns to be used as the new index.
58855881
append : bool, default False
58865882
Whether to append columns to existing index.
5883+
Setting to True will add the new columns to existing index.
5884+
When set to False, the current index will be dropped from the DataFrame.
58875885
inplace : bool, default False
58885886
Whether to modify the DataFrame rather than creating a new one.
58895887
verify_integrity : bool, default False
@@ -5957,6 +5955,25 @@ def set_index(
59575955
2 4 4 2014 40
59585956
3 9 7 2013 84
59595957
4 16 10 2014 31
5958+
5959+
Append a column to the existing index:
5960+
5961+
>>> df.set_index("month", inplace=True)
5962+
>>> df.set_index("year", append=True)
5963+
sale
5964+
month year
5965+
1 2012 55
5966+
4 2014 40
5967+
7 2013 84
5968+
10 2014 31
5969+
5970+
>>> df.set_index("year", append=False)
5971+
sale
5972+
year
5973+
2012 55
5974+
2014 40
5975+
2013 84
5976+
2014 31
59605977
"""
59615978
inplace = validate_bool_kwarg(inplace, "inplace")
59625979
self._check_inplace_and_allows_duplicate_labels(inplace)

0 commit comments

Comments
 (0)