MAX

11/13/16

The MAX function returns the maximum 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

Maximum of column values

Maximum of all columns


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

In [4]:
df.max()
Out[4]:
Name                     Valter
Date                    2/10/16
Item                 Red Apples
Product Category          Pears
Time (hrs)                    8
Amt Picked (lbs)             15
Sale Value ($/lb)           3.8
dtype: object

Maximum of multiple columns (opt-in method)

In [5]:
columns = ['Time (hrs)', 'Amt Picked (lbs)']
df[columns].max()
Out[5]:
Time (hrs)           8
Amt Picked (lbs)    15
dtype: int64

Maximum of multiple columns (opt-out method)

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

Maximum of one specific column

All rows

In [8]:
df['Amt Picked (lbs)'].max()
Out[8]:
15

Range of rows

In [9]:
df.ix[3:4]['Amt Picked (lbs)'].max()
Out[9]:
15

Maximum for each individual person


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

Multiple columns

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

Single column

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

Maximum 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

Maximum of specific rows (opt-in method)

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

Maximum of specific rows (opt-out method)

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