0

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. enter image description here

I want to change

1

2

3

11 . . .

to

0

1

2

3

4 . . .

Thanks.

6
  • Is your dataframe is Pandas Dtaframe? Commented Feb 18, 2019 at 19:23
  • @DavidDR yes it is in Pandas Commented Feb 18, 2019 at 19:24
  • 1
    Use .reset_index() Commented Feb 18, 2019 at 19:24
  • @IMCoins this will make another column to to the data frame. is there any way to remove the previous index?they are still there Commented Feb 18, 2019 at 19:28
  • 1
    df=df.reset_index(drop=True) Commented Feb 18, 2019 at 19:29

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.