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
How to get the max/min value in Pandas DataFrame when nan value in it
提问by GoingMyWay
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?
如何跳过该nan
值并获得该列的最大值?
采纳答案by Divakar
回答by Alex
回答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 = True
when calling max
:
您可以numeric_only = True
在调用时设置max
:
df.iloc[:, 1].max(numeric_only = True)
回答by Ken Dekalb
When the df contains NaN
values it reports NaN
values, 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']})