简体   繁体   中英

Re-index Pandas Dataframe - python

I have a data frame and I droped some part of it. now my new data frame has not all the rows if we consider data frame as a table. 在此处输入图片说明

I want to change

1

2

3

11 . . .

to

0

1

2

3

4 . . .

Thanks.

Use reset_index() with the optional parameter drop=True

import pandas as pd

df = pd.DataFrame({
        'A0' : list(range(10)),
        'A1' : list(range(10)),
        'A2' : list(range(10)),
        '3A' : list(range(10)),
        'A4' : list(range(10)),
        'A5' : list(range(10))
    })
print(df.head())
#    A0  A1  A2  3A  A4  A5
# 0   0   0   0   0   0   0
# 1   1   1   1   1   1   1
# 2   2   2   2   2   2   2
# 3   3   3   3   3   3   3
# 4   4   4   4   4   4   4

df = df.iloc[2:4]
print(df)
#    A0  A1  A2  3A  A4  A5
# 2   2   2   2   2   2   2
# 3   3   3   3   3   3   3

df = df.reset_index(drop=True)
print(df)
#    A0  A1  A2  3A  A4  A5
# 0   2   2   2   2   2   2
# 1   3   3   3   3   3   3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM