How to find highest combination in dataframe [duplicate]

偶尔善良 提交于 2021-02-11 13:47:07

问题


I have a data frame that has repeating values in 2 columns and I only want to keep the highest value of each combination. For the following data frame:

df = pd.DataFrame(
np.array([['A', 'B ', 3], ['A', 'B', 6], ['C', 'D', 9],  ['C', 'D', 2], ['C', 'B', 4]]))
df

how would I get this dataframe as a result:

|A|B|6|
|C|D|9|
|C|B|4|

回答1:


Use groupby and aggregate max:

df.groupby([0,1], as_index=False)[2].max() 

Here's a post with a similar use case.



来源:https://stackoverflow.com/questions/61721971/how-to-find-highest-combination-in-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!