How to get the max/min value in Pandas DataFrame when nan value in it

You can set numeric_only = True when calling max:

df.iloc[:, 1].max(numeric_only = True)

Attention:

For everyone trying to use it with pandas.series This is not working nevertheless it is mentioned in the docs

See post on github


if you dont use iloc or loc, it is simple as:

df['column'].max()

or

df['column'][df.index.min():df.index.max()]

or any kind of range in this second square brackets


You can use NumPy's help with np.nanmax, np.nanmin :

In [28]: df
Out[28]: 
   A   B  C
0  7 NaN  8
1  3   3  5
2  8   1  7
3  3   0  3
4  8   2  7

In [29]: np.nanmax(df.iloc[:, 1].values)
Out[29]: 3.0

In [30]: np.nanmin(df.iloc[:, 1].values)
Out[30]: 0.0

You can use Series.dropna.

res = df.iloc[:, 1].dropna().max()

Tags:

Python

Pandas