Skip to content Skip to sidebar Skip to footer

Find Min Value Column And Min Value Column Name In Python DataFrame

I have a matrix like below and I need to create 2 more columns with minimum value from columns COL01-04 and name of that column (exluding NaN): In[1]: matrix Out[1]: ID CO

Solution 1:

Use DataFrame.min and DataFrame.idxmin with axis=1 for check values per rows:

c = ['COL01', 'COL02', 'COL03', 'COL04']
matrix[c] = matrix[c].apply(lambda x: pd.to_numeric(x, errors='coerce'))

matrix['Min_val'] = matrix[c].min(axis=1)
matrix['Min_col'] = matrix[c].idxmin(axis=1)

Or for new columns use DataFrame.assign:

matrix = matrix.assign(Min_val = matrix[c].min(axis=1), Min_col=matrix[c].idxmin(axis=1))

print (matrix)
   ID   COL01  COL02   COL03    COL04  Min_val Min_col
0   1     NaN   1662  1583.0  1697.40   1583.0   COL03
1   2     NaN   1006  1476.0  1018.44   1006.0   COL02
2   3  1452.0   1487  2197.5  1516.27   1452.0   COL01
3   4     NaN   1554  2298.0  1585.62   1554.0   COL02

Solution 2:

you can try this :

def get_col(sr):
    name=sr.idxmin()
    value = sr[name]
    return pd.Series([value, name])
df[['Min_val','Min_col']] = df[['COL01','COL02','COL03','COL04']].apply(lambda x : get_col(x), axis=1)

df

     ID   COL01  COL02   COL03    COL04  Min_val Min_col
0  0001     NaN   1662  1583.0  1697.40   1583.0   COL03
1  0002     NaN   1006  1476.0  1018.44   1006.0   COL02
2  0003  1452.0   1487  2197.5  1516.27   1452.0   COL01
3  0004     NaN   1554  2298.0  1585.62   1554.0   COL02

Post a Comment for "Find Min Value Column And Min Value Column Name In Python DataFrame"