Get Column Names For The N Max/min Values Per Row In Pandas
I am trying to get, for each individual row, the name of the column with the max/min value up to N-values. Given something like this: a b c d e 1.2 2 0.1 0.
Solution 1:
You can use nlargest and nsmallest:
In [11]: res = df.apply(lambda x: pd.Series(np.concatenate([x.nlargest(2).index.values, x.nsmallest(2).index.values])), axis=1)
In [12]: res
Out[12]:
01230 b a e c
1 d e b a
2 c b a d
3 b a c e
In [13]: df[["Max1", "Max2", "Min1", "Min2"]] = res
In [14]: df
Out[14]:
a b c d e Max1 Max2 Min1 Min2
01.22.00.100.80.01 b a e c
12.11.13.204.63.40 d e b a
20.21.98.800.31.30 c b a d
33.37.80.123.21.40 b a c e
Solution 2:
If the order of the largest/smallest and second largest/smallest values don't matter, then you can use np.argpartition
.
N = 2# Number of min/max values u = np.argpartition(df, axis=1, kth=N).values
v = df.columns.values[u].reshape(u.shape)
maxdf = pd.DataFrame(v[:,-N:]).rename(columns=lambda x: f'Max{x+1}')
mindf = pd.DataFrame(v[:,:N]).rename(columns=lambda x: f'Min{x+1}')
pd.concat([df, maxdf, mindf], axis=1)
a b c d e Max1 Max2 Min1 Min2
01.22.00.100.80.01 b a e c
12.11.13.204.63.40 d e b a
20.21.98.800.31.30 b c a d
33.37.80.123.21.40 a b c e
Post a Comment for "Get Column Names For The N Max/min Values Per Row In Pandas"