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 useBut there is also
df.assign(Index=range(len(df))).set_index('Index'), which I don't usually useAnd, 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.
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?.reset_index(drop=True)is the way to go. If you wanted to overwrite the index, then.set_indexwould be my choice, but there is no reason to.assignto the dataframe first. Simply create a newpd.Index(or subclass/equivalent) and pass that to.set_indexdirectly.