Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I"m not sure how to reset index after 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 ..

like image 503
JPC Avatar asked Nov 23 '16 03:11

JPC


People also ask

How do I reset a DataFrame index?

Use DataFrame.reset_index() function We can use DataFrame. reset_index() to reset the index of the updated DataFrame. By default, it adds the current row index as a new column called 'index' in DataFrame, and it will create a new row index as a range of numbers starting at 0.

How do you reset the index while concatenating two Dataframes?

You can reset the index using concat() function as well. Pass in the argument ignore_index=True to the concat() function. If you have only one dataframe whose index has to be reset, then just pass that dataframe in the list to the concat() function.

What does Reset index do in Pandas?

reset_index in pandas is used to reset index of the dataframe object to default indexing (0 to number of rows minus 1) or to reset multi level index. By doing so, the original index gets converted to a column.

How do I delete a Pandas DataFrame index?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.


1 Answers

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:

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.

like image 125
John Zwinck Avatar answered Sep 20 '22 13:09

John Zwinck