You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python:如何获取列中空值与非空值的频次及占比

按空值/非空值统计数据并生成汇总表格

原始数据集

col1
100
NaN
100
NaN
200
100
NaN
150

实现代码(Python + Pandas)

import pandas as pd

# 构建数据集
data = {'col1': [100, None, 100, None, 200, 100, None, 150]}
df = pd.DataFrame(data)

# 统计核心指标
total = len(df)
non_null_cnt = df['col1'].count()
null_cnt = total - non_null_cnt

non_null_pct = (non_null_cnt / total) * 100
null_pct = (null_cnt / total) * 100

# 生成结果表格
result = pd.DataFrame(
    {
        'Frequency': [non_null_cnt, null_cnt],
        'Percent': [non_null_pct, null_pct],
        'Cumulative Frequency': [non_null_cnt, total],
        'Cumulative Percent': [non_null_pct, 100.0]
    },
    index=['Non-Null', 'Null']
)

print(result)

输出结果

FrequencyPercentCumulative FrequencyCumulative Percent
Non-Null562.5562.5
Null337.58100

内容的提问来源于stack exchange,提问作者jii0302

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 00:05:25