Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C(从数据框中删除重复项,基于两列 A,B,在另一列 C 中保持具有最大值的行)
问题描述
我有一个 pandas 数据框,其中包含根据两列(A 和 B)的重复值:
I have a pandas dataframe which contains duplicates values according to two columns (A and B):
A B C
1 2 1
1 2 4
2 7 1
3 4 0
3 4 8
我想删除在 C 列中保持最大值的行的重复项.这将导致:
I want to remove duplicates keeping the row with max value in column C. This would lead to:
A B C
1 2 4
2 7 1
3 4 8
我不知道该怎么做.我应该使用 drop_duplicates() 吗?
I cannot figure out how to do that. Should I use drop_duplicates(), something else?
推荐答案
你可以使用 group by:
You can do it using group by:
c_maxes = df.groupby(['A', 'B']).C.transform(max)
df = df.loc[df.C == c_maxes]
c_maxes 是每个组中 C 最大值的Series,但长度和索引相同df.如果您还没有使用过 .transform,那么打印 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.
使用 drop_duplicates 的另一种方法是
Another approach using drop_duplicates would be
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.
从 pandas 0.18 开始,第二个解决方案是
From pandas 0.18 up the second solution would be
df.sort_values('C').drop_duplicates(subset=['A', 'B'], keep='last')
或者,或者,
df.sort_values('C', ascending=False).drop_duplicates(subset=['A', 'B'])
无论如何,groupby 解决方案的性能似乎要好得多:
In any case, the groupby solution seems to be significantly more performing:
%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
这篇关于从数据框中删除重复项,基于两列 A,B,在另一列 C 中保持具有最大值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从数据框中删除重复项,基于两列 A,B,在另一
基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
