熊猫:独特的数据帧

问题描述 投票:32回答:2

我有一个具有重复行的DataFrame。我想获得一个具有唯一索引且没有重复项的DataFrame。丢弃重复的值是可以的。这可能吗?它是由groupby完成的吗?

python pandas
2个回答
63
投票
In [29]: df.drop_duplicates()
Out[29]: 
   b  c
1  2  3
3  4  0
7  5  9

10
投票

通过阅读split-apply-combine文档示例找出了一种方法。

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
© www.soinside.com 2019 - 2024. All rights reserved.