Dean Taler
Dean Taler

Reputation: 773

How to find maximum value depends on two columns?

what I have?

I have a dataframe looks like that:

      id1   id2      max_value   
0      1     3       50748.0     
1      1     3       50631.0     
2      1     4       55876.0     
3      1     4       56424.0     
4      1     5       28242.0     
5      1     5       28316.0     

what I want to get?

for each id1, id2 I want to get the max_value. for the example above:

      id1   id2      max_value   
0      1     3       50748.0     
1      1     4       56424.0     
2      1     5       28316.0     

what I have tried?

I tried to use groupby inside groupby but without success

Upvotes: 0

Views: 48

Answers (1)

Dean Taler
Dean Taler

Reputation: 773

Answer

I found the right answer: groupby with 2 columns

df = df.groupby(['id1', 'id2'])['max_value'].agg('max').reset_index()

Upvotes: 1

Related Questions