[英]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.