In [1]:
import pandas as pd
import numpy as np

In [2]:
df = pd.DataFrame({'col1': [2, 3, 1, 3, 3, 4],
                   'col2': [30, 10, 10, 40, 40, 20]},
                  index=['A', 'B', 'C', 'D', 'E', 'F'])
print(df)


   col1  col2
A     2    30
B     3    10
C     1    10
D     3    40
E     3    40
F     4    20

In [3]:
s = df['col1']
print(s)


A    2
B    3
C    1
D    3
E    3
F    4
Name: col1, dtype: int64

In [4]:
print(df.max())


col1     4
col2    40
dtype: int64

In [5]:
print(type(df.max()))


<class 'pandas.core.series.Series'>

In [6]:
print(df.min())


col1     1
col2    10
dtype: int64

In [7]:
print(type(df.min()))


<class 'pandas.core.series.Series'>

In [8]:
print(df.max(axis=1))


A    30
B    10
C    10
D    40
E    40
F    20
dtype: int64

In [9]:
print(df.min(axis=1))


A    2
B    3
C    1
D    3
E    3
F    4
dtype: int64

In [10]:
print(s.max())


4

In [11]:
print(type(s.max()))


<class 'numpy.int64'>

In [12]:
print(s.min())


1

In [13]:
print(type(s.min()))


<class 'numpy.int64'>

In [14]:
print(s.nlargest(4))


F    4
B    3
D    3
E    3
Name: col1, dtype: int64

In [15]:
print(type(s.nlargest(4)))


<class 'pandas.core.series.Series'>

In [16]:
print(s.nsmallest(4))


C    1
A    2
B    3
D    3
Name: col1, dtype: int64

In [17]:
print(type(s.nsmallest(4)))


<class 'pandas.core.series.Series'>

In [18]:
print(s.nlargest(1))


F    4
Name: col1, dtype: int64

In [19]:
print(type(s.nlargest(1)))


<class 'pandas.core.series.Series'>

In [20]:
print(df.nlargest(4, 'col1'))


   col1  col2
F     4    20
B     3    10
D     3    40
E     3    40

In [21]:
print(type(df.nlargest(4, 'col1')))


<class 'pandas.core.frame.DataFrame'>

In [22]:
print(df.nsmallest(4, 'col1'))


   col1  col2
C     1    10
A     2    30
B     3    10
D     3    40

In [23]:
print(type(df.nsmallest(4, 'col1')))


<class 'pandas.core.frame.DataFrame'>

In [24]:
print(df.nlargest(1, 'col1'))


   col1  col2
F     4    20

In [25]:
print(type(df.nlargest(1, 'col1')))


<class 'pandas.core.frame.DataFrame'>

In [26]:
print(df.nlargest(4, ['col1', 'col2']))


   col1  col2
F     4    20
D     3    40
E     3    40
B     3    10

In [27]:
print(df.nlargest(4, ['col2', 'col1']))


   col1  col2
D     3    40
E     3    40
A     2    30
F     4    20

In [28]:
print(df.nsmallest(4, 'col1'))


   col1  col2
C     1    10
A     2    30
B     3    10
D     3    40

In [29]:
print(df.nsmallest(4, 'col1', keep='first'))


   col1  col2
C     1    10
A     2    30
B     3    10
D     3    40

In [30]:
print(df.nsmallest(4, 'col1', keep='last'))


   col1  col2
C     1    10
A     2    30
E     3    40
D     3    40

In [31]:
print(df.nsmallest(4, 'col1', keep='all'))


   col1  col2
C     1    10
A     2    30
B     3    10
D     3    40
E     3    40

In [32]:
print(df.nsmallest(3, ['col1', 'col2'], keep='all'))


   col1  col2
C     1    10
A     2    30
B     3    10

In [33]:
print(df.nsmallest(4, ['col1', 'col2'], keep='all'))


   col1  col2
C     1    10
A     2    30
B     3    10
D     3    40
E     3    40

In [34]:
print(df['col1'].nsmallest(4).tolist())


[1, 2, 3, 3]

In [35]:
print(type(df['col1'].nsmallest(4).tolist()))


<class 'list'>

In [36]:
print(df['col1'].nsmallest(4).to_numpy())


[1 2 3 3]

In [37]:
print(type(df['col1'].nsmallest(4).to_numpy()))


<class 'numpy.ndarray'>

In [38]:
print(df['col1'].nsmallest(4).values)


[1 2 3 3]

In [39]:
print(type(df['col1'].nsmallest(4).values))


<class 'numpy.ndarray'>

In [40]:
print(df['col1'].nsmallest(3))


C    1
A    2
B    3
Name: col1, dtype: int64

In [41]:
print(df['col2'].nsmallest(3))


B    10
C    10
F    20
Name: col2, dtype: int64

In [42]:
print([df[col_name].nsmallest(3).tolist() for col_name in df])


[[1, 2, 3], [10, 10, 20]]

In [43]:
print({col_name: col.nsmallest(3).tolist() for col_name, col in df.iteritems()})


{'col1': [1, 2, 3], 'col2': [10, 10, 20]}

In [44]:
print(np.array([df[col_name].nsmallest(3).tolist() for col_name in df]))


[[ 1  2  3]
 [10 10 20]]

In [45]:
print([df[col_name].nsmallest(3, keep='all').tolist() for col_name in df])


[[1, 2, 3, 3, 3], [10, 10, 20]]

In [46]:
print({col_name: col.nsmallest(3, keep='all').tolist() for col_name, col in df.iteritems()})


{'col1': [1, 2, 3, 3, 3], 'col2': [10, 10, 20]}

In [47]:
print(np.array([df[col_name].nsmallest(3, keep='all').tolist() for col_name in df]))


[list([1, 2, 3, 3, 3]) list([10, 10, 20])]