简体   繁体   English

如何基于两列删除重复数据,从而删除熊猫数据框中第三列中最大的列?

[英]How to remove duplicates based on two columns removing the the largest of 3rd column in pandas dataframe?

Suppose I have a pandas dataframe that is like this: 假设我有一个像这样的pandas数据框:

df=
A  B  6  2
A  C  4  2
D  F  9  3
K  L  8  9
A  B  4  3
D  F  8  2

How can I say, if columns A and B have duplicates remove the ones that have the largest column C? 我该怎么说,如果A列和B列重复,则删除C列最大的列?

So for instance we can see lines 1 and 5 have the same columns A and B. 因此,例如,我们可以看到第1行和第5行具有相同的A和B列。

A  B  6  2 (Line 1)
A  B  4  3 (Line 5)

I want to remove line 1 as 6 is greater than 4. 我想删除第1行,因为6大于4。

So my output should be 所以我的输出应该是

A  C  4  2
K  L  8  9
A  B  4  3
D  F  8  2

Try sorting the column in descending order on which you need to find max value using pd.sort_values 尝试按降序对列进行排序,您需要使用pd.sort_values查找最大值

Then drop_duplicates using pd.drop_duplicate 然后使用pd.drop_duplicate

df.sort_values(by=['C'],ascending=[True],inplace=True)
df.drop_duplicates(subset=['A','B'],inplace=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两列并获取 python pandas dataframe 中两列中所有匹配项的第三列的平均值? - how to compare two columns and get the mean value of the the 3rd column for all matching items in the two in python pandas dataframe? 根据两列的值删除数据帧pandas中的重复项 - Remove duplicates in dataframe pandas based on values of two columns 根据熊猫数据框第 3 列中的条件,按天分组的 2 列的加权平均值 - Weighted average, grouped by day, of 2 columns based on criteria in 3rd column of pandas dataframe 比较来自相同 pandas dataframe 的 2 列的值和基于比较的第 3 列的返回值 - comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison 在Pandas DataFrame中比较2列并填充第3列 - Comparing 2 columns in Pandas DataFrame and populating a 3rd column Python Pandas - 检查两列中的值,对第三列求和 - Python Pandas - check value in two columns, sum the 3rd column 熊猫按两列分组,并从第三列输出值 - Pandas groupby two columns and output values from 3rd column 如何通过对第三列中的值求和,将前两列中具有相同值的 Pandas Dataframe 的行组合在一起? - How to group together rows of Pandas Dataframe with same values in first 2 columns by summing values in the 3rd column? 第 3 列 pandas python 中至少有两列 - Minimum of two columns in a 3rd column pandas python 根据 Pandas 中两列的组合删除重复项 - Remove duplicates based on combination of two columns in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM