本文介绍了Python-根据列的最大值删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对大熊猫不是很满意,我认为大熊猫应该解决我的问题:我有一个文本文件,其中包含数据(id1; id2; value1; value2; value3)

I am not really good with pandas, and I think pandas should solve my problem:I have a text file, that contains data (id1;id2;value1;value2;value3)

1;2;30;40;20.3;
1;2;30;42;26.2;
3;5;12;55;10.7;
3;5;12;23;8.7;
3;5;12;33;11.2;
24;12;1;553;1.1;
24;12;1;23;1.9;

因此,我想保留id1id2value1和更高的value3相等的行. Value2并不重要,但需要保留,例如

As a result, I want to keep lines, that have equal id1, id2, value1, and higher value3. Value2 is not important, but it needs to be kept, e.g.

1;2;30;42;26.2;
3;5;12;33;11.2;
24;12;1;23;1.9; 

推荐答案

您需要 DataFrameGroupBy.idxmax 表示最大值为value3的索引,然后通过DataFrame -docs/stable/generated/pandas.DataFrame.loc.html"rel =" nofollow noreferrer> loc :

You need DataFrameGroupBy.idxmax for indexes of max value of value3 and thes select DataFrame by loc:

print (df.groupby(['id1','id2','value1']).value3.idxmax())
id1  id2  value1
1    2    30        1
3    5    12        4
24   12   1         6
Name: value3, dtype: int64

df = df.loc[df.groupby(['id1','id2','value1']).value3.idxmax()]
print (df)
   id1  id2  value1  value2  value3   a
1    1    2      30      42    26.2 NaN
4    3    5      12      33    11.2 NaN
6   24   12       1      23     1.9 NaN

另一种可能的解决方案是 sort_values 按列value3,然后按groupby GroupBy.first :

Another possible solution is sort_values by column value3 and then groupby with GroupBy.first:

df = df.sort_values('value3', ascending=False)
       .groupby(['id1','id2','value1'], sort=False)
       .first()
       .reset_index()
print (df)
   id1  id2  value1  value2  value3   a
0    1    2      30      42    26.2 NaN
1    3    5      12      33    11.2 NaN
2   24   12       1      23     1.9 NaN

这篇关于Python-根据列的最大值删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 09:28