[英]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?
[英]How to remove duplicates based on two columns removing the the largest of 3rd column in pandas dataframe?
假设我有一个像这样的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
我该怎么说,如果A列和B列重复,则删除C列最大的列?
因此,例如,我们可以看到第1行和第5行具有相同的A和B列。
A B 6 2 (Line 1)
A B 4 3 (Line 5)
我想删除第1行,因为6大于4。
所以我的输出应该是
A C 4 2
K L 8 9
A B 4 3
D F 8 2
尝试按降序对列进行排序,您需要使用pd.sort_values
查找最大值
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.