1

I have a dataframe

>>> df
zeroa  zerob zeroc zerod zeroe zero
FSi                                     
1       10    100     a    ok   NaN   ok
1       11    110        temp   NaN  NaN
2       12    120     c  temp   NaN  NaN
3      NaN    NaN   NaN   NaN    ok  NaN

I want to keep only unique indexes, as index 1 is repeating I want its second instance to be dropped, how could I do that ? I want my result as

>>> df
     zeroa  zerob zeroc zerod zeroe zero
FSi                                     
1       10    100     a    ok   NaN   ok
2       12    120     c  temp   NaN  NaN
3      NaN    NaN   NaN   NaN    ok  NaN
pratish_v
  • 137
  • 1
  • 14
  • 2
    While this question is a duplicate, the accepted answer is not the best answer IMO, so here is a link to the [best answer](http://stackoverflow.com/a/34297689/5276797). – IanS Sep 21 '16 at 11:40

2 Answers2

2

Without resetting the index:

df[~df.index.duplicated()]
IanS
  • 15,771
  • 9
  • 60
  • 84
1

Ok something like this should help:

df = df.reset_index().drop_duplicates(subset='FSi', keep='first').set_index('FSi')

Explanation: First we reset_index which creates a column FSi cause drop_duplicates works on columns and not on index. We keep the first one and set_index again back to FSi

milos.ai
  • 3,882
  • 7
  • 31
  • 33