0

Hello Stackoverflow community!

I have attemped to find an answer to my question but without success. I am basically trying to replicate the MinIF formula from excel, but so far I have only managed to do it for "1" row at a time. Here is an example of what I am trying to achieve:

Please see attached link

The idea is, that for all products with the same subgroup, the lowest value/price for that group is found, and inserted in a new column next to it. I hope i am being specific enough, I am relatively new to python, and I have literally no idea how to even start...

help much appreciated, thank you.

1 Answer 1

1

I think you need transform:

df['SubGroupLowestPrice'] = df.groupby('SubGroup')['Price'].transform('min')

Sample:

df = pd.DataFrame({'A':[1,2,3,4,5,5],
                   'SubGroup':[1,5,5,6,6,6],
                   'Price':[7,8,9,10,2,3]})

print (df)
   A  Price  SubGroup
0  1      7         1
1  2      8         5
2  3      9         5
3  4     10         6
4  5      2         6
5  5      3         6

df['SubGroupLowestPrice'] = df.groupby('SubGroup')['Price'].transform('min')
print (df)
   A  Price  SubGroup  SubGroupLowestPrice
0  1      7         1                    7
1  2      8         5                    8
2  3      9         5                    8
3  4     10         6                    2
4  5      2         6                    2
5  5      3         6                    2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.