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.
>>> d
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()
When the df contains NaN
values it reports NaN
values, Using
np.nanmax(df.values)
gave the desired answer.
Dataframe aggregate function.agg()
will automatically ignore NaN value.
df.agg({'income':'max'})
Besides, it can also be use together with .groupby
df.groupby('column').agg({'income':['max','mean']})
You can set numeric_only = True
when calling max
:
df.iloc[:, 1].max(numeric_only = True)
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