J Westwood
J Westwood

Reputation: 431

get second largest value in row in selected columns in dataframe in pandas

I have a dataframe with subset of it shown below. There are more columns to the right and left of the ones I am showing you

M_cols  10D_MA  30D_MA  50D_MA  100D_MA 200D_MA       Max   Min   2nd smallest
        68.58    70.89   69.37   **68.24**   64.41   70.89   64.41   68.24 
        **68.32**71.00   69.47   68.50   64.49       71.00   64.49   68.32 
        68.57 **68.40**  69.57   71.07   64.57       71.07   64.57   68.40 

I can get the min (and max is easy as well) with the following code

df2['MIN'] = df2[['10D_MA','30D_MA','50D_MA','100D_MA','200D_MA']].max(axis=1)

But how do I get the 2nd smallest. I tried this and got the following error

df2['2nd SMALLEST'] = df2[['10D_MA','30D_MA','50D_MA','100D_MA','200D_MA']].nsmallest(2)

TypeError: nsmallest() missing 1 required positional argument: 'columns'

Seems like this should be a simple answer but I am stuck

Upvotes: 0

Views: 3445

Answers (1)

BENY
BENY

Reputation: 323226

For example you have following df

df=pd.DataFrame({'V1':[1,2,3],'V2':[3,2,1],'V3':[3,4,9]})

After pick up the value need to compare , we just need to sort value by axis=0(default)

sortdf=pd.DataFrame(np.sort(df[['V1','V2','V3']].values))
sortdf
Out[419]: 
   0  1  2
0  1  3  3
1  2  2  4
2  1  3  9

1st max:

sortdf.iloc[:,-1]
Out[421]: 
0    3
1    4
2    9
Name: 2, dtype: int64

2nd max

sortdf.iloc[:,-2]
Out[422]: 
0    3
1    2
2    3
Name: 1, dtype: int64

Upvotes: 5

Related Questions