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
?
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
In [29]: df.drop_duplicates()
Out[29]:
b c
1 2 3
3 4 0
7 5 9
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
?
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
In [29]: df.drop_duplicates()
Out[29]:
b c
1 2 3
3 4 0
7 5 9