Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reindexing dataframes

Tags:

I have a data frame. Then I have a logical condition using which I create another data frame by removing some rows. The new data frame however skips indices for removed rows. How can I get it to reindex sequentially without skipping? Here's a sample coded to clarify

import pandas as pd import numpy as np  jjarray = np.array(range(5)) eq2 = jjarray == 2 neq2 = np.logical_not(eq2)  jjdf = pd.DataFrame(jjarray) jjdfno2 = jjdf[neq2]  jjdfno2 

Out:

  0 0 0 1 1 3 3 4 4 

I want it to look like this:

  0 0 0 1 1 2 3 3 4 

Thanks.

like image 535
user2133151 Avatar asked Mar 21 '13 20:03

user2133151


People also ask

What is reindex in DataFrame?

The reindex() method allows you to change the row indexes, and the columns labels. ;] Note: The values are set to NaN if the new index is not the same as the old.

How do you reindex a DataFrame in Python?

One can reindex a single column or multiple columns by using reindex() method and by specifying the axis we want to reindex. Default values in the new index that are not present in the dataframe are assigned NaN. Example #1: Python3.

What does reindex mean in Python?

Reindexing changes the row labels and column labels of a DataFrame. To reindex means to conform the data to match a given set of labels along a particular axis. Multiple operations can be accomplished through indexing like − Reorder the existing data to match a new set of labels.

What is reindex function?

REINDEX rebuilds an index using the data stored in the index's table, replacing the old copy of the index. There are several scenarios in which to use REINDEX : An index has become corrupted, and no longer contains valid data.


1 Answers

One way is to use reset_index:

>>> df = pd.DataFrame(range(5)) >>> eq2 = df[0] == 2 >>> df_no_2 = df[~eq2] >>> df_no_2    0 0  0 1  1 3  3 4  4 >>> df_no_2.reset_index(drop=True)    0 0  0 1  1 2  3 3  4 
like image 184
DSM Avatar answered Sep 18 '22 12:09

DSM