2

I've been using Python and Pandas for years and know it well, but I use the reset_index() feature a lot and I'd say 95% of the time, also use drop = True as a keyword. If I'm resetting the index, I think it's a good bet that I don't want to keep the old index in my dataframe. Is there a way to set drop = True as the default, and if I don't want to drop it, instead use drop = False?

I looked at the pd.set_option() documentation and didn't see what I was looking for.

2
  • Changing the source code? pandas/core/frame.py and pandas/core/series.py; finding the reset_index method in there and changing default value of keyword argument drop. Commented May 24, 2021 at 17:36
  • That might be the only way. I know there are some options you can change that don't require changing the source code, thus the question. But thanks for the paths, I'll check it out. Commented May 24, 2021 at 17:45

1 Answer 1

3

You can add a new reset_index method:

pd.DataFrame.my_reset_index = lambda self: self.reset_index(drop=True)
df = df.my_reset_index()
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, and that could be done directly within the module? No need to mess with the source code? I don't love that I'd have to update literally hundreds of reset_index() in my code, but that's definitely an option.

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.