reset_index¶
By default reset_index
returns a copy rather than modify the original data frame.
You can specify inplace = True
to overwrite the behavior.
Series¶
- If you drop the original index, you still have a Series. However, if you reset index of a sereis without dropping the original index, you get a data frame.
In [5]:
s = pd.Series([1, 2, 3, 4], index=["r1", "r2", "r3", "r4"])
s
Out[5]:
In [8]:
df = s.reset_index()
df
Out[8]:
In [10]:
df = s.reset_index(drop=True)
df
Out[10]:
DataFrame¶
In [15]:
import pandas as pd
df = pd.DataFrame(
{"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]}, index=["r1", "r2", "r3", "r4", "r5"]
)
df.head()
Out[15]:
In [29]:
# keep the original index as a new column and create a new index
df.reset_index()
Out[29]:
In [30]:
# drop the original index and create a new index
df.reset_index(drop=True)
Out[30]:
Multi-index¶
In [31]:
import pandas as pd
df = pd.DataFrame(
{"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]},
index=pd.MultiIndex.from_tuples(
[("r1", 0), ("r2", 1), ("r3", 2), ("r4", 3), ("r5", 4)]
),
)
df.head()
Out[31]:
In [32]:
df.reset_index()
Out[32]:
In [33]:
df.reset_index(drop=True)
Out[33]:
In [38]:
# drops the 2nd index and keep the first index
df.reset_index(level=1, drop=True)
Out[38]: