如何获取Pandas DataFrame中的最大值/最小值

时间:2016-07-21 15:26:33

标签: python pandas

由于我的pandas数据帧的一列有nan值,所以当我想获得该列的最大值时,它只返回错误。

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

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

6 个答案:

答案 0 :(得分:10)

您可以使用Series.dropna

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

答案 1 :(得分:9)

您可以np.nanmax np.nanmin使用NumPy的帮助:

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

答案 2 :(得分:1)

当df包含NaN个值时,它会报告NaN个值,使用     np.nanmax(df.values)给出了理想的答案。

答案 3 :(得分:1)

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

此外,它还可以与.groupby

一起使用

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

答案 4 :(得分:0)

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

df['column'].max()

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

或第二个方括号中的任意范围

答案 5 :(得分:0)

您可以在致电numeric_only = True时设置max

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