How to reset pandas dataframe index after dropna () pandas dataframe

I'm not sure how to reset the index after dropna ()

df_all = df_all.dropna() df_all.reset_index(drop=True) 

but after the rollback index skips, for example, a jump from 0,1,2,4 ..

+7
python pandas dataframe
source share
1 answer

The code you posted already does what you want, but doesn’t do it "in place." Try adding inplace=True to reset_index() or reassign the result to df_all . Note that you can also use inplace=True with dropna() , therefore:

 df_all.dropna(inplace=True) df_all.reset_index(drop=True, inplace=True) 

Is everything in place. Or,

 df_all = df_all.dropna() df_all = df_all.reset_index(drop=True) 

reassign df_all .

+8
source share

All Articles