Python(pandas):基于两列删除重复项,在另一列中保留具有最大值的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32093829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python(pandas): removing duplicates based on two columns keeping row with max value in another column
提问by Elsalex
I have a dataframe which contains duplicates values according to two columns (A and B):
我有一个数据框,其中包含根据两列(A 和 B)的重复值:
A B C
1 2 1
1 2 4
2 7 1
3 4 0
3 4 8
I want to remove duplicates keeping the row with max value in column C. This would lead to:
我想删除重复项,保留 C 列中具有最大值的行。这将导致:
A B C
1 2 4
2 7 1
3 4 8
I cannot figure out how to do that. Should I use drop_duplicates()
, something else?
我不知道该怎么做。我应该使用drop_duplicates()
其他东西吗?
采纳答案by JoeCondron
You can do it using group by:
您可以使用 group by 来完成:
c_maxes = df.groupby(['A', 'B']).C.transform(max)
df = df.loc[df.C == c_maxes]
c_maxes
is a Series
of the maximum values of C
in each group but which is of the same length and with the same index as df
. If you haven't used .transform
then printing c_maxes
might be a good idea to see how it works.
c_maxes
是每个组Series
中 的最大值C
但与 具有相同的长度和相同的索引df
。如果您还没有使用过,.transform
那么打印c_maxes
可能是一个好主意,看看它是如何工作的。
Another approach using drop_duplicates
would be
另一种使用drop_duplicates
方法是
df.sort('C').drop_duplicates(subset=['A', 'B'], take_last=True)
Not sure which is more efficient but I guess the first approach as it doesn't involve sorting.
不确定哪个更有效,但我猜是第一种方法,因为它不涉及排序。
EDIT:From pandas 0.18
up the second solution would be
编辑:从pandas 0.18
第二个解决方案是
df.sort_values('C').drop_duplicates(subset=['A', 'B'], keep='last')
or, alternatively,
或者,或者,
df.sort_values('C', ascending=False).drop_duplicates(subset=['A', 'B'])
In any case, the groupby
solution seems to be significantly more performing:
无论如何,该groupby
解决方案的性能似乎要好得多:
%timeit -n 10 df.loc[df.groupby(['A', 'B']).C.max == df.C]
10 loops, best of 3: 25.7 ms per loop
%timeit -n 10 df.sort_values('C').drop_duplicates(subset=['A', 'B'], keep='last')
10 loops, best of 3: 101 ms per loop
回答by b10n
I think groupby should work.
我认为 groupby 应该有效。
df.groupby(['A', 'B']).max()['C']
If you need a dataframe back you can chain the reset index call.
如果您需要返回数据帧,您可以链接重置索引调用。
df.groupby(['A', 'B']).max()['C'].reset_index()
回答by AlexT
You can do it with drop_duplicates
as you wanted
你可以drop_duplicates
随心所欲
# initialisation
d = pd.DataFrame({'A' : [1,1,2,3,3], 'B' : [2,2,7,4,4], 'C' : [1,4,1,0,8]})
d = d.sort_values("C", ascending=False)
d = d.drop_duplicates(["A","B"])
If it's important to get the same order
如果获得相同的订单很重要
d = d.sort_index()
回答by Sudharsan
You can do this simply by using pandas drop duplicates function
您可以简单地使用熊猫删除重复项功能来做到这一点
df.drop_duplicates(['A','B'],keep= 'last')