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 ..
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With