问题
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