Pandas: unique dataframe

Question:

I have a DataFrame that has duplicated rows. I’d like to get a DataFrame with a unique index and no duplicates. It’s ok to discard the duplicated values. Is this possible? Would it be a done by groupby?

Asked By: Adam Greenhall

||

Answers:

Figured out one way to do it by reading the split-apply-combine documentation examples.

df = pandas.DataFrame({'b':[2,2,4,5], 'c': [3,3,0,9]}, index=[1,1,3,7])
df_unique = df.groupby(level=0).first()

df
   b  c
1  2  3
1  2  3
3  4  0
7  5  9

df_unique
   b  c
1  2  3
3  4  0
7  5  9
Answered By: Adam Greenhall
In [29]: df.drop_duplicates()
Out[29]: 
   b  c
1  2  3
3  4  0
7  5  9
Answered By: Wouter Overmeire
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.