0

I was wondering why reset_index() has no effect in the following piece of code.

data = [0,10,20,30,40,50]
df = pd.DataFrame(data, columns=['Numbers'])
df.drop(df.index[2:4], inplace=True)
df.reset_index()
df

   Numbers
0        0
1       10
4       40
5       50

UPDATE:

If I use df.reset_index(inplace=True), I see a new column which is not desired.

   index  Numbers
0      0        0
1      1       10
2      4       40
3      5       50
mahmood
  • 23,197
  • 49
  • 147
  • 242

2 Answers2

3

Because reset_index() has inplace=False as default, so you need to do reset_index(inplace=True). Docs

Minh-Long Luu
  • 2,393
  • 1
  • 17
  • 39
0

Please try this code

import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({'column_name': [1, 2, 0, 4, 0, 6]})

# drop rows where column 'column_name' has value of 0
df = df[df['column_name'] != 0]

# reset the index of the resulting DataFrame
df = df.reset_index(drop=True)

print(df)