Consider the following sample data:
import numpy as np
import pandas as pd
np.random.seed(123)
n = 200
df = pd.DataFrame({'v1': np.random.randint(0, 100, n),
'v2': np.random.randint(10, 90, n),
'v3': np.random.randint(10, 90, n),
'another_v': np.random.randint(10, 45, n)})
In this data, I would like to add a new column minval
with the smallest value in ['v1', 'v2', 'v3']
and another one mincol
with the column indicator of the column containing this minimal value.
I do this using the following code, which works fine.
df['minval'] = df[['v1', 'v2', 'v3']].min(axis='columns') # minimum value
df['mincol'] = df[['v1', 'v2', 'v3']].idxmin(axis = 'columns').str[1:]
However, how can I get the same for the second-smallest value?
S. below for expected output of a sample
v1 v2 v3 another_v 2minval 2mincol
0 66 55 30 37 55 2
1 92 68 22 44 68 2
2 98 69 36 38 69 2
3 17 33 71 15 33 2
4 83 35 49 23 49 3
5 57 37 62 38 57 1
6 86 67 89 37 86 1
7 97 13 49 19 49 3
8 96 88 18 13 88 2
9 47 69 36 33 47 1
(Similar questions suggesting nlargest
etc. only provide solutions for within-column comparisons.)