Home > Mobile >  Pandas what is the best way row sum without min & max value
Pandas what is the best way row sum without min & max value

Time:08-18

From the evaluation data below, I would like to get sum per row except min and max values.

       col_a     col_b     col_c     col_d     col_e

P0       1         7         2         5         2
P1       5         2         3         2         5
P2       2         0         1         4         2

Expected:

       col_mean
P0       3       # mean(2, 5, 2)
P1       3.33    # mean(2, 3, 5), if there are two max (min) value, except just one
P2       1.66    # mean(2, 1, 2)

What is the best way to get this?

CodePudding user response:

Something like this should meet your requirements. Sort them row-wise in ascending order, then take the average of columns b-d

df.values.sort(1)
df.iloc[:,1:-1].mean(1)

Output

P0    3.000000
P1    3.333333
P2    1.666667

CodePudding user response:

You can try using:

df['mean'] = (df.sum(axis=1) - (df.min(axis=1)   df.max(axis=1))) / (df.shape[1]-2)

Which returns:

   col_a  col_b  col_c  col_d  col_e      mean
0      1      7      2      5      2  3.000000
1      5      2      3      2      5  3.333333
2      2      0      1      4      2  1.666667

CodePudding user response:

Another way:

f = lambda x: (x.sum() - x.max() - x.min())/(len(x)-2) if len(x) > 2 else 0
df.apply(f,axis=1)
  • Related