Find 2nd lowest value in Python

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
886
This generates the minimum value; how would I go about calculating the 2nd lowest value?

import pandas as pd
ct_mean = pd.crosstab(df['Check-In'], df['Date Captured'],
values=df['Current price'], aggfunc='min')

Many thanks.
 
This defaults to the 1st min if the 2nd min does not exist.
Book1
ABCDEFGH
1Check-InDate CapturedCurrent priceDataFrameDate Captured1/1/20241/2/2024
2A1/1/202410Check-In
3A1/1/202420A20nan
4B1/1/202430B30nan
5C1/2/20245Cnan15
6C1/2/202415
Sheet1
Cell Formulas
RangeFormula
E1E1=PY(import pandas as pd df = xl("A1:C6", headers=True) ct_mean = pd.crosstab( df['Check-In'], df['Date Captured'], values=df['Current price'], aggfunc=lambda x: x.nsmallest(2).iloc[-1] if len(x) > 1 else x.min() )
F1:H5F1=E1.arrayPreview
Dynamic array formulas.
 
Upvote 0
Thanks, can't quite get it to work; I push E1 to be an array, but also my data is:

df=xl("Data", headers=True)
 
Upvote 0
Is Data a named range or a table object?
 
Upvote 0
Can you show what's the error and the formula you've entered?
 
Upvote 0

Forum statistics

Threads
1,226,797
Messages
6,193,051
Members
453,772
Latest member
aastupin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top