由于我的pandas数据帧的一列有nan
值,所以当我想获得该列的最大值时,它只返回错误。
>>> df.iloc[:, 1].max()
'error:512'
如何跳过该nan
值并获取该列的最大值?
答案 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)