MIN

11/13/16

The MIN function returns the minimum value included in a range.

In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('sample_data.csv')
In [3]:
df
Out[3]:
Name Date Item Product Category Time (hrs) Amt Picked (lbs) Sale Value ($/lb)
0 Lucas 1/5/16 Green Apples Apples 2 5 2.5
1 Valter 1/5/16 European Pear Pears 4 10 2.0
2 Erik 1/8/16 Red Apples Apples 2 3 2.1
3 Georg 2/1/16 Asian Pear Pears 8 15 3.8
4 Lucas 2/10/16 Red Apples Apples 6 6 3.6

Minimum of column values

Minimum of all columns


Will also return minimum value for string-typed columns (based on alphabetical order)

In [5]:
df.min()
Out[5]:
Name                       Erik
Date                     1/5/16
Item                 Asian Pear
Product Category         Apples
Time (hrs)                    2
Amt Picked (lbs)              3
Sale Value ($/lb)             2
dtype: object

Minimum of multiple columns (opt-in method)

In [6]:
columns = ['Time (hrs)', 'Amt Picked (lbs)']
df[columns].min()
Out[6]:
Time (hrs)          2
Amt Picked (lbs)    3
dtype: int64

Minimum of multiple columns (opt-out method)

In [7]:
columns = list(df)
columns
Out[7]:
['Name',
 'Date',
 'Item',
 'Product Category',
 'Time (hrs)',
 'Amt Picked (lbs)',
 'Sale Value ($/lb)']
In [8]:
removelist = ['Name', 'Date', 'Item', 'Product Category', 'Sale Value ($/lb)']
newlist = [value for value in columns if value not in removelist]
df[newlist].min()
Out[8]:
Time (hrs)          2
Amt Picked (lbs)    3
dtype: int64

Minimum of one specific column

All rows

In [9]:
df['Amt Picked (lbs)'].min()
Out[9]:
3

Range of rows

In [11]:
df.ix[3:4]['Amt Picked (lbs)'].min()
Out[11]:
6

Minimum for each individual person


NB: reset_index( ) is optional and used here to preserve the spreadsheet formatting

Multiple columns

In [33]:
df.groupby('Name')[['Time (hrs)','Amt Picked (lbs)']].min().reset_index()
Out[33]:
Name Time (hrs) Amt Picked (lbs)
0 Erik 2 3
1 Georg 8 15
2 Lucas 2 5
3 Valter 4 10

Single column

In [31]:
df.groupby('Name')['Time (hrs)'].min().reset_index()
Out[31]:
Name Time (hrs)
0 Erik 2
1 Georg 8
2 Lucas 2
3 Valter 4

Minimum of row values


Useful if the dataset has been transposed

In [12]:
df.T
Out[12]:
0 1 2 3 4
Name Lucas Valter Erik Georg Lucas
Date 1/5/16 1/5/16 1/8/16 2/1/16 2/10/16
Item Green Apples European Pear Red Apples Asian Pear Red Apples
Product Category Apples Pears Apples Pears Apples
Time (hrs) 2 4 2 8 6
Amt Picked (lbs) 5 10 3 15 6
Sale Value ($/lb) 2.5 2 2.1 3.8 3.6
In [13]:
df_alt = df.T

Minimum of specific rows (opt-in method)

In [14]:
rows = ['Time (hrs)', 'Amt Picked (lbs)']
In [15]:
df_alt.ix[rows].min(axis=1)
Out[15]:
Time (hrs)          2
Amt Picked (lbs)    3
dtype: float64

Minimum of specific rows (opt-out method)

In [16]:
rows = list(df_alt.index)
In [17]:
rows
Out[17]:
['Name',
 'Date',
 'Item',
 'Product Category',
 'Time (hrs)',
 'Amt Picked (lbs)',
 'Sale Value ($/lb)']
In [18]:
removelist = ['Name', 'Date', 'Item', 'Product Category', 'Sale Value ($/lb)']
newlist = [value for value in rows if value not in removelist]
df[newlist].min()
Out[18]:
Time (hrs)          2
Amt Picked (lbs)    3
dtype: int64