2

I want to find the minimum value per row and create a new column indicating which of those columns has the lowest number. Unfortunately, it seems like pandas isn't immediately able to help in this regard. My research has led to the min() function, which does find the lowest for each row (when axis=1), but there's no further information beyond the number itself.

initialDict = {"A":[6.53,11.47,92.08],"B":[9.11,8.15,12.49]}
initialDf = pd.DataFrame.from_dict(initialDict,orient="index",columns=["Value 1","Value 2","Value 3"])
>>> initialDf
   Value 1  Value 2  Value 3
A     6.53    11.47    92.08
B     9.11     8.15    12.49
>>> initialDf.min(axis=1,numeric_only=True)
A   6.53 # Value 1 - just a number is useless to me.
B   8.15 # Value 2 - how do i access which columns these are?

My sample data is a lot larger than two rows, so ideally I'd want a vectorised solution.

Can I somehow access which column has the lowest number per row and assign it to a new value?

0

1 Answer 1

3

A possible solution, which uses idxmin:

initialDf.assign(min = initialDf.idxmin(axis=1))

Output:

   Value 1  Value 2  Value 3      min
A     6.53    11.47    92.08  Value 1
B     9.11     8.15    12.49  Value 2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.