[英]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.