首页 > 解决方案 > 计算 pandas DataFrame 中的所有 NaN

问题描述

我正在尝试计算 pandas 系列中的 NaN 元素(数据类型类 'numpy.float64'),以了解有多少数据类型是类 'pandas.core.series.Series'

这是为了计算熊猫系列中的空值

import pandas as pd
oc=pd.read_csv(csv_file)
oc.count("NaN")

我的预期输出为oc,count("NaN")7 但它显示'Level NaN must be same as name (None)'

标签: pythonpandasdataframe

解决方案


The argument to count isn't what you want counted (it's actually the axis name or index).

You're looking for df.isna().values.sum() (to count NaNs across the entire DataFrame), or len(df) - df['column'].count() (to count NaNs in a specific column).


推荐阅读