2

I need the min distance per member (ID). My input is like this:

ID      ColA        DistA   DistB   DistC   DistD
1001    SomeValueA  11.08   12.07   9.89    19.77
1002    SomeValueB  16.04   55.78   17.55   3.55
1003    SomeValueC  12.09   16.78   5.44    27.44

Expected result would be:

ID      ColA        MinDistance
1001    SomeValueA  9.89
1002    SomeValueB  3.55
1003    SomeValueC  5.44

I'm thinking about groupby, but this works in the wrong direction. I need aggregate on a row basis.

Any help would be appreciated.

Kind regards, M.

1 Answer 1

6

Use filter for get only column with Dist with min function:

df['MinDistance'] = df.filter(like='Dist').min(axis=1)

Or if possible select columns by positions use iloc:

df['MinDistance'] = df.iloc[:, 2:].min(axis=1)
Sign up to request clarification or add additional context in comments.

2 Comments

In one line. :) Should have learned python instead of SQL. Thanks so much!
@SQL_M - Each of this languages is better for something else ;)

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.