Python 如何在 Pandas DataFrame 中获得 nan 值时的最大值/最小值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38508294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:03:31  来源:igfitidea点击:

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

pythonpandas

提问by GoingMyWay

Since one column of my pandas dataframe has nanvalue, 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 nanvalue and get the max value of that column?

如何跳过该nan值并获得该列的最大值?

采纳答案by Divakar

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

您可以使用NumPy的有帮助np.nanmaxnp.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

回答by Alex

You can use Series.dropna.

您可以使用Series.dropna

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

回答by Gilco

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

如果您不使用 iloc 或 loc,则很简单:

df['column'].max()

or

或者

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

or any kind of range in this second square brackets

或第二个方括号中的任何类型的范围

回答by user5054

You can set numeric_only = Truewhen calling max:

您可以numeric_only = True在调用时设置max

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

回答by Ken Dekalb

When the df contains NaNvalues it reports NaNvalues, Using np.nanmax(df.values)gave the desired answer.

当 df 包含NaN它报告的NaN值时,Using np.nanmax(df.values)给出了所需的答案。

回答by YoongKang Lim

Dataframe aggregate function.agg()will automatically ignore NaN value. df.agg({'income':'max'})

Dataframe 聚合函数.agg()将自动忽略 NaN 值。 df.agg({'income':'max'})

Besides, it can also be use together with .groupby

此外,它还可以与 .groupby

df.groupby('column').agg({'income':['max','mean']})

df.groupby('column').agg({'income':['max','mean']})