0

I have the following codes. I would like to know if the modified dataframe can be started from 0,1,2... so on like regular indexing on Panda.

df = pd.DataFrame([4, 4, 3, 4, 1])
df2 = df[2:4]

where df2 is now-

     0
0    3
2    3

If I would like to access df2 in some regular indexing convention from 0..n, I can't with this approach. Is there some other way that can be done?

Thanks in advance.

5
  • 1
    Try df2 = df2.reset_index() Commented Mar 13, 2018 at 13:45
  • 1
    In my opinion need select by position e.g. print (df2.iloc[[0]]) Commented Mar 13, 2018 at 13:47
  • @jezrael, i don't think this is a duplicate of the post you linked to. it seems the OP needs guidance on how to use reset_index Commented Mar 13, 2018 at 13:49
  • 1
    @HaleemurAli - If yes, was added dupe for .reset_index() too. Commented Mar 13, 2018 at 13:51
  • @HaleemurAli - Both solutions working, but if use iloc for select reset_index is not necessary. Commented Mar 13, 2018 at 13:59

1 Answer 1

2

You can reset the index using reset_index():

df2 = df2.reset_index(drop=True)

For your data, this prints:

>>> print(df2.reset_index(drop=True))
   0
0  3
1  4
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.