Python 如何在 dropna() 熊猫数据帧后重置索引熊猫数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40755680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to reset index pandas dataframe after dropna() pandas dataframe
提问by JPC
I"m not sure how to reset index after dropna()
我不知道如何在 dropna() 后重置索引
df_all = df_all.dropna()
df_all.reset_index(drop=True)
but after drop row index would skip for example jump from 0,1,2,4 ..
但是在删除行索引后会跳过例如从 0,1,2,4 跳转..
回答by John Zwinck
The code you've posted already does what you want, but does not do it "in place." Try adding inplace=True
to reset_index()
or else reassigning the result to df_all
. Note that you can also use inplace=True
with dropna()
, so:
您发布的代码已经完成了您想要的操作,但并未“就地”完成。尝试添加inplace=True
到reset_index()
或将结果重新分配给df_all
。请注意,您也可以使用inplace=True
with dropna()
,因此:
df_all.dropna(inplace=True)
df_all.reset_index(drop=True, inplace=True)
Does it all in place. Or,
是否都到位。或者,
df_all = df_all.dropna()
df_all = df_all.reset_index(drop=True)
to reassign df_all
.
重新分配df_all
.