Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In pandas, how can I reset index without adding a new column?

Tags:

python

pandas

In [37]: df = pd.DataFrame([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]])  In [38]: df2 = pd.concat([df, df])  In [39]: df2.reset_index() Out[39]:     index  0  1  2  3 0      0  1  2  3  4 1      1  2  3  4  5 2      2  3  4  5  6 3      0  1  2  3  4 4      1  2  3  4  5 5      2  3  4  5  6 

My problem is that how can I reset_index without adding a new column index?

like image 717
waitingkuo Avatar asked Apr 23 '13 11:04

waitingkuo


People also ask

How do I keep the original Panda index?

If you want to keep the original index as a column, use reset_index() to reassign the index to a sequential number starting from 0 . You can change the index to a different column by using set_index() after reset_index() . See also the following article for reset_index() .

How do you reset the column index of a DataFrame in Python?

To reset the index in pandas, you simply need to chain the function . reset_index() with the dataframe object. On applying the . reset_index() function, the index gets shifted to the dataframe as a separate column.

What does resetting the index do?

The reset_index() function is used to generate a new DataFrame or Series with the index reset. For a Series with a MultiIndex, only remove the specified levels from the index. Removes all levels by default. Just reset the index, without inserting it as a column in the new DataFrame.


1 Answers

You can use the drop=True option in reset_index(). See here.

like image 113
bdiamante Avatar answered Sep 20 '22 00:09

bdiamante