search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

Pandas DataFrame | max method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Basic and Descriptive Statistics
schedule Mar 10, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.max(~) method computes the maximum of each column or row of the DataFrame.

Parameters

1. axislink | int or string | optional

Whether to compute the maximum row-wise or column-wise:

Axis

Description

Maximum is computed for each column.

"index" or 0

Maximum is computed for each row.

"columns" or 1

By default, axis=0.

2. skipnalink | boolean | optional

Whether or not to skip NaN. By default, skipna=True.

3. level | string or int | optional

The name or the integer index of the level to consider. This is only relevant if your DataFrame is Multi-index.

4. numeric_onlylink | None or boolean | optional

The allowed values are as follows:

Value

Description

True

Only numeric rows/columns will be considered (e.g. float, int, boolean).

False

Attempt computation with all types (e.g. strings and dates), and throw an error whenever the maximum cannot be computed.

None

Attempt computation with all types, and ignore all rows/columns whose maximum cannot be computed without raising an error.

Note that a maximum can only be computed when the > operator is well-defined between the types.

By default, numeric_only=None.

Return Value

If the level parameter is specified, then a DataFrame will be returned. Otherwise, a Series will be returned.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
A B
0 2 4
1 3 5

Column-wise maximum

To compute the maximum for each column:

df.max() # or axis=0
A 3
B 5
dtype: int64

Row-wise maximum

To compute the maximum for each row, set axis=1:

df.max(axis=1)
0 4
1 5
dtype: int64

Specifying skipna

Consider the following DataFrame with a missing value:

df = pd.DataFrame({"A":[4,pd.np.nan]})
df
A
0 4.0
1 NaN

By default, skipna=True, which means that missing values will be ignored:

df.max() # skipna=True
A 4.0
dtype: float64

To consider missing values:

df.max(skipna=False)
A NaN
dtype: float64

Note that the maximum of a row/column that contains a missing value will be NaN.

Specifying numeric_only

Consider the following DataFrame:

df = pd.DataFrame({"A":[4,5], "B":[2,True], "C":["6",False]})
df
A B C
0 4 2 "6"
1 5 True False

Here, both columns B and C contain mixed types, but the key difference is that the maximum is defined for B, but not for C. Computing the maximum requires comparison operators (< and >) between the types to be well-defined.

Recall that the internal representation of a True boolean is 1, so the operation 2>True is defined:

2 > True
True

On the other hand, "6">False throws an error:

"6" > False
TypeError: '>' not supported between instances of 'str' and 'bool'

None

By default, numeric_only=None, which means that rows/columns with mixed types will also be considered:

df.max(numeric_only=None)
A 5
B 2
dtype: int64

Here, notice how the maximum was computed for column B, but not for C. By passing in None, rows/columns where the maximum cannot be computed (due to undefined < and > between types) will simply be ignored without raising an error.

False

By setting numeric_only=False, rows/columns with mixed types will again be considered, but an error will be thrown when the maximum cannot be computed:

df.max(numeric_only=False)
TypeError: '<=' not supported between instances of 'str' and 'bool'

Here, we end up with an error because column C contains mixed types where < operation is not defined.

True

By setting numeric_only=True, only numeric rows/columns will be considered:

df.max(numeric_only=True)
A 5
dtype: int64

Notice how columns B and C were ignored this time since they contain non-numeric types.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...