【发布时间】:2012-09-01 14:43:01
【问题描述】:
我有一个重复行的 DataFrame。我想获得一个具有唯一索引且没有重复项的 DataFrame。可以丢弃重复的值。这可能吗?会不会是由groupby 完成的?
【问题讨论】:
我有一个重复行的 DataFrame。我想获得一个具有唯一索引且没有重复项的 DataFrame。可以丢弃重复的值。这可能吗?会不会是由groupby 完成的?
【问题讨论】:
In [29]: df.drop_duplicates()
Out[29]:
b c
1 2 3
3 4 0
7 5 9
【讨论】:
df.drop_duplicates(inplace=True)。
通过阅读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
【讨论】:
unstack 时,我得到了 ValueError: Index contains duplicate entries, cannot reshape,但这个解决方案只适用于我必须这样做 df_unique = df.groupby(level=[0,1]).first()