0

So lets say I have a DataFrame:

    stuff  temp
id             
1       3  20.0
1       6  20.1
1       7  21.4
2       1  30.2
2       3   0.0
2       2  34.0
3       7   0.0
3       6   0.0
3       2  14.4

And I want to drop the index; what method is better to use?

  • There is df.reset_index(drop=True), which is what I will usually use

  • But there is also df.assign(Index=range(len(df))).set_index('Index'), which I don't usually use

  • And, is there any other methods?

Well, I want to find the most efficient/best way to drop the index of a pd.DataFrame. Can you give me a clear explanation. I'm doing a efficient code-writing project and I want to know the best options. Thanks.

5
  • Your index here "id" looks like it has useful information, you probably want to move this index "id" into the dataframe as a column. Do this. 'df = df.reset_index()`, you will get a new dataframe index as the default range index from 0 to len of the dataframe minus 1. Commented Dec 28, 2022 at 15:57
  • Ok, but if I had a useless column: lets say name, and I want to delete it (because id already identifies it), what would the difference be between the options: quicker/slower? reliable/non-reliable? Commented Dec 28, 2022 at 16:03
  • 1
    Why haven't you tried timing those methods? Commented Dec 28, 2022 at 16:04
  • I want to know if the methods are reliable as well, but good point. Commented Dec 28, 2022 at 16:06
  • 1
    I would always go with a built-in method instead of writing the code myself. In this case, .reset_index(drop=True) is the way to go. If you wanted to overwrite the index, then .set_index would be my choice, but there is no reason to .assign to the dataframe first. Simply create a new pd.Index (or subclass/equivalent) and pass that to .set_index directly. Commented Dec 28, 2022 at 16:33

0

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.