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

Goi*_*Way 12 python pandas

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

>>> df.iloc[:, 1].max()
'error:512'
Run Code Online (Sandbox Code Playgroud)

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

Div*_*kar 12

您可以使用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
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 10

您可以使用Series.dropna.

res = df.iloc[:, 1].dropna().max()
Run Code Online (Sandbox Code Playgroud)