Home > Enterprise >  Maintaining indexing in dataframe after dropping the duplicate rows
Maintaining indexing in dataframe after dropping the duplicate rows

Time:02-23

I have dropped duplicate from the dataframe so indexing is changed and if I want to access df['Color'][1] it is showing error so how can i maintain it ??

import pandas as pd

boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle']
        }
df = pd.DataFrame(boxes, columns = ['Color', 'Shape'])

df = df.drop_duplicates()
print(df)

Output


   Color      Shape
0  Green  Rectangle
2  Green     Square
3   Blue  Rectangle
4   Blue     Square
5    Red     Square
7    Red  Rectangle

How I wanted

   Color      Shape
0  Green  Rectangle
1  Green     Square
2   Blue  Rectangle
3   Blue     Square
4    Red     Square
5    Red  Rectangle

CodePudding user response:

Use ignore_index=True as parameter of drop_duplicates:

>>> df.drop_duplicates(ignore_index=True)
   Color      Shape
0  Green  Rectangle
1  Green     Square
2   Blue  Rectangle
3   Blue     Square
4    Red     Square
5    Red  Rectangle
  • Related