Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
nan
>>> df.iloc[:, 1].max() 'error:512'
How can I skip that nan value and get the max value of that column?
You can use NumPy's help with np.nanmax, np.nanmin :
NumPy
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
Add a comment
You can use Series.dropna.
res = df.iloc[:, 1].dropna().max()
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 set numeric_only = True when calling max:
numeric_only = True
max
df.iloc[:, 1].max(numeric_only = True)
For everyone trying to use it with pandas.series This is not working nevertheless it is mentioned in the docs
pandas.series
See post on github
Dataframe aggregate function.agg() will automatically ignore NaN value. df.agg({'income':'max'})
.agg()
df.agg({'income':'max'})
Besides, it can also be use together with .groupby
.groupby
df.groupby('column').agg({'income':['max','mean']})
When the df contains NaN values it reports NaN values, Using np.nanmax(df.values) gave the desired answer.
NaN
np.nanmax(df.values)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
nanby default - see docs.