Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

Since one column of my pandas dataframe has nan value, so when I want to get the max value of that column, it just return error.

>>> df.iloc[:, 1].max()
'error:512'

How can I skip that nan value and get the max value of that column?

like image 261
GoingMyWay Avatar asked Jul 21 '16 15:07

GoingMyWay


People also ask

How pandas handle DataFrame NaN values?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

Does pandas median ignore NaN?

median() Method to Find Median Ignoring NaN Values. We use the default value of skipna parameter i.e. skipna=True to find the median of DataFrame along the specified axis by ignoring NaN values. If we set skipna=True , it ignores the NaN in the dataframe.

How do you get max and min in pandas?

Pandas DataFrame min() Method The min() method returns a Series with the minimum value of each column. By specifying the column axis ( axis='columns' ), the max() method searches column-wise and returns the minimum value for each row.

How to get the maximum value in a pandas Dataframe?

You can use the pandas max () function to get the maximum value in a given column, multiple columns, or the entire dataframe. The following is the syntax: # df is a pandas dataframe # max value in a column

What is the difference between Min and max function of pandas?

Here the pandas min () function is used for finding the minimum value of specified axis. In this example of pandas min () function, we will be using level parameter. Here the minimum value of each level is shown in the output. The max function of pandas helps us in finding the maximum values on specified axis.

How to check for Nan in pandas Dataframe?

Here are 4 ways to check for NaN in Pandas DataFrame: (1) Check for NaN under a single DataFrame column: df ['your column name'].isnull ().values.any () (2) Count the NaN under a single DataFrame column:

How do I find the minimum value in a Dataframe column?

Similar to that, we can use the idxmin function to search and find the minimum value in a column: The smallest number in the column x1 is the number 1. The previous example has explained how to get the maxima and minima of a pandas DataFrame column. This example shows how to find the row index positions that correspond to the max and min values.


2 Answers

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
like image 56
Divakar Avatar answered Sep 20 '22 20:09

Divakar


You can use Series.dropna.

res = df.iloc[:, 1].dropna().max()
like image 34
Alex Avatar answered Sep 20 '22 20:09

Alex