df.shape[0] - df.isnull().sum()
时间: 2024-06-01 09:10:53 浏览: 22
This code returns the number of non-null values for each column in a pandas dataframe.
- `df.shape[0]` returns the number of rows in the dataframe.
- `df.isnull()` returns a boolean dataframe with the same shape as `df`, where `True` indicates a missing value (i.e. `NaN`).
- `df.isnull().sum()` returns a series object with the sum of missing values for each column.
- Subtracting the sum of missing values from the total number of rows gives the number of non-null values for each column.
The output is a series object where the index represents the column names and the values represent the number of non-null values in each column.
相关问题
train_df = pd.read_csv('./data/train.csv', index_col = 0) test_df = pd.read_csv('./data/test.csv', index_col = 0) print("Number of rows: ", train_df.shape[0]) print("Number of columns: ", train_df.shape[1]) display(train_df) train_df.info() print("Number of rows: ", test_df.shape[0]) print("Number of columns: ", test_df.shape[1]) display(test_df) test_df.info() train_df.describe() # Check for NaN values and the number of unique values print("Are there any NaN values: ", train_df.isnull().values.any()) print(train_df.nunique()) # Count NaN values in each column nan_counts = train_df.isna().sum() # Print the counts print(nan_counts)
这段代码是用来读取两个csv文件,并对它们进行一些数据分析的。具体来说,它首先读取了一个名为train.csv的文件和一个名为test.csv的文件,并打印了它们的行数和列数。然后,它显示了train_df和test_df的内容和信息,包括每个列的数据类型和非空值的数量。接下来,它使用describe()函数对train_df进行描述统计,包括平均值、标准差、最小值、最大值等。最后,它检查train_df中是否有NaN值,并计算每列中的NaN值的数量,并打印出来。
# 查看缺失值数量与比例 ( pd.DataFrame({ "NaN_num": round(df.isnull().sum(),2), "NaN_percent":(df.isnull().sum()/df.shape[0]).apply(lambda x:str(round(x*100,2))+'%') , }) .sort_values('NaN_num', ascending=False) )翻译这段代码
这段代码是用来查看一个 pandas 数据框中每一列缺失值的数量和占比的。具体来说,它使用了 pandas 中的 isnull() 函数来检查每个元素是否为空值(NaN),然后使用 sum() 函数统计每列中 NaN 值的数量。接着,计算每列的缺失值占整个数据框的比例,并使用 apply() 函数将比例转换为字符串格式,并加上百分号。最后,将结果按照缺失值数量从大到小排序,得到一个新的数据框。整个过程中,使用了 round() 函数来保留小数点后两位。