How to change only the maximum value of a group in pandas dataframe
You can use idxmax()
to get the idx of the maximum for each group, and increment only these items, like this:
max_idxs = df.groupby(['Item'])['Count'].idxmax()
df['New_Count']=df['Count'] # copy entire column
df['New_Count'][max_idxs]+=1 # increment only the maximum item for each group by 1
Use idxmax
:
idx = df.groupby("Item")["Count"].idxmax()
df["New_Count"] = df["Count"]
df.loc[idx, "New_Count"] += 1
This will only increment the first occurrence of th maximum in each group.
If you want to increment all the maximum values in the case of a tie, you can use transform
instead. Just replace the first line above with:
idx = df.groupby("Item")["Count"].transform(max) == df["Count"]
Here's another way not using groupby but using duplicated
df.loc[~df.sort_values('Count', ascending=False).duplicated('Item'), 'Count'] += 1
Output:
Item Count
0 A 61
1 A 20
2 A 21
3 B 34
4 B 33
5 B 32