SQL_M
SQL_M

Reputation: 2475

Min value per row, Python Pandas

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.

Upvotes: 2

Views: 928

Answers (1)

jezrael
jezrael

Reputation: 862511

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)

Upvotes: 6

Related Questions