Home > Blockchain >  Set value when row is maximum in group by - Python Pandas
Set value when row is maximum in group by - Python Pandas

Time:02-03

I am trying to create a column (is_max) that has either 1 if a column B is the maximum in a group of values of column A or 0 if it is not.

Example:

[Input]

A B 
1 2
2 3
1 4
2 5

[Output]

A B is_max
1 2   0 
2 5   0
1 4   1
2 3   0

What I'm trying:

df['is_max'] = 0
df.loc[df.reset_index().groupby('A')['B'].idxmax(),'is_max'] = 1

CodePudding user response:

I make assumption A is your group now that you did not state

df['is_max']=(df['B']==df.groupby('A')['B'].transform('max')).astype(int)

or

df1.groupby('A')['B'].apply(lambda x: x==x.max()).astype(int)

CodePudding user response:

Fix your code by remove the reset_index

df['is_max'] = 0
df.loc[df.groupby('A')['B'].idxmax(),'is_max'] = 1
df
Out[39]: 
   A  B  is_max
0  1  2       0
1  2  3       0
2  1  4       1
3  2  5       1
  •  Tags:  
  • Related