简体   繁体   English

Pandas reset_index() - 将默认值更改为删除索引

[英]Pandas reset_index() - change default to drop index

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.多年来我一直在使用 Python 和 Pandas 并且非常了解它,但是我经常使用reset_index()功能,我会说 95% 的时间,也使用drop = True作为关键字。 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.如果我正在重置索引,我认为我不想将旧索引保留在我的 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 ?有没有办法将drop = True设置为默认值,如果我不想删除它,请改用drop = False

I looked at the pd.set_option() documentation and didn't see what I was looking for.我查看了pd.set_option()文档并没有看到我在寻找什么。

You can add a new reset_index method:您可以添加一个新的reset_index方法:

pd.DataFrame.my_reset_index = lambda self: self.reset_index(drop=True)
df = df.my_reset_index()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM