Ben Chuanlong Du's Blog

It is never too late to learn.

Reset the Index of a pandas DataFrame

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

  1. 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]:
r1    1
r2    2
r3    3
r4    4
dtype: int64
In [8]:
df = s.reset_index()
df
Out[8]:
index 0
0 r1 1
1 r2 2
2 r3 3
3 r4 4
In [10]:
df = s.reset_index(drop=True)
df
Out[10]:
0    1
1    2
2    3
3    4
dtype: int64

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]:
x y
r1 1 5
r2 2 4
r3 3 3
r4 4 2
r5 5 1
In [29]:
# keep the original index as a new column and create a new index
df.reset_index()
Out[29]:
index x y
0 r1 1 5
1 r2 2 4
2 r3 3 3
3 r4 4 2
4 r5 5 1
In [30]:
# drop the original index and create a new index
df.reset_index(drop=True)
Out[30]:
x y
0 1 5
1 2 4
2 3 3
3 4 2
4 5 1

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]:
x y
r1 0 1 5
r2 1 2 4
r3 2 3 3
r4 3 4 2
r5 4 5 1
In [32]:
df.reset_index()
Out[32]:
level_0 level_1 x y
0 r1 0 1 5
1 r2 1 2 4
2 r3 2 3 3
3 r4 3 4 2
4 r5 4 5 1
In [33]:
df.reset_index(drop=True)
Out[33]:
x y
0 1 5
1 2 4
2 3 3
3 4 2
4 5 1
In [38]:
# drops the 2nd index and keep the first index
df.reset_index(level=1, drop=True)
Out[38]:
x y
r1 1 5
r2 2 4
r3 3 3
r4 4 2
r5 5 1

Comments