How to find top N minimum values from the DataFrame, Python-3
You can make use of nsmallest(..)
[pandas-doc]:
df.nsmallest(2, 'Age')
For the given sample data, this gives us:
>>> df.nsmallest(2, 'Age') Name Age0 A 184 E 23
Or if you only need the value of the Age
column:
>>> df['Age'].nsmallest(2)0 184 23Name: Age, dtype: int64
or you can wrap it in a list:
>>> df['Age'].nsmallest(2).to_list()[18, 23]
You can obtain the n smallest unique values, by first constructing a Series
with unique values:
>>> pd.Series(df['Age'].unique()).nsmallest(2)0 184 23dtype: int64>>> df['Age'].drop_duplicates().nsmallest(2)0 184 23Name: Age, dtype: int64
The right thing is to use nsmallest
, here I show another way: DataFrame.sort_values
+ DataFrame.head
df['Age'].sort_values().head(2).tolist()#[18, 23]
UPDATED
If there are duplicates, we could use Series.drop_duplicates
previously:
df['Age'].drop_duplicates().nsmallest(2).tolist()#df['Age'].drop_duplicates().sort_values().head(2).tolist()#[18, 23]
[*np.sort(df['Age'].unique())[:2]]#[18, 23]