Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a row in a Pandas DataFrame and relabel the index?

I am reading a file into a Pandas DataFrame that may have invalid (i.e. NaN) rows. This is sequential data, so I have row_id+1 refer to row_id. When I use frame.dropna(), I get the desired structure, but the index labels stay as they were originally assigned. How can the index labels get reassigned 0 to N-1 where N is the number of rows after dropna()?

like image 909
user394430 Avatar asked Dec 10 '12 19:12

user394430


People also ask

How do I delete a row in a DataFrame using index?

To drop a row or column in a dataframe, you need to use the drop() method available in the dataframe. You can read more about the drop() method in the docs here. Rows are labelled using the index number starting with 0, by default.

How do you relabel rows in Pandas?

Pandas rename() method is used to rename any index, column or row. Renaming of column can also be done by dataframe. columns = [#list] .

How do I delete a row in Pandas DataFrame?

By using pandas. DataFrame. drop() method you can drop/remove/delete rows from DataFrame. axis param is used to specify what axis you would like to remove.


1 Answers

In addition to an accepted answer:

You should also use inplace=True as well:

df.reset_index(drop=True, inplace=True)
like image 66
Artem Fediai Avatar answered Oct 10 '22 21:10

Artem Fediai