question.it
question.it

Reputation: 2968

How to find top N minimum values from the DataFrame, Python-3

I have below Dataframe with Field 'Age', Needs find to top 3 minimum age from the DataFrame

DF = pd.DataFrame.from_dict({'Name':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], 'Age':[18, 45, 35, 70, 23, 24, 50, 65, 18, 23]})

DF['Age'].min()  

Want top two Age i.e 18, 23 in List, How to achieve this?

Note: DataFrame - DF Contains Age Duplicates i.e 18 & 23 repeated twice, need unique values.

Upvotes: 11

Views: 3748

Answers (2)

ansev
ansev

Reputation: 30920

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]

or np.sort + np.unique

[*np.sort(df['Age'].unique())[:2]]
#[18, 23]

Upvotes: 3

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476534

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  Age
0    A   18
4    E   23

Or if you only need the value of the Age column:

>>> df['Age'].nsmallest(2)
0    18
4    23
Name: 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    18
4    23
dtype: int64
>>> df['Age'].drop_duplicates().nsmallest(2)
0    18
4    23
Name: Age, dtype: int64

Upvotes: 17

Related Questions