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

如何用更简洁通用的方法找出Pandas DataFrame含缺失值的列

More Concise & Efficient Ways to Find Columns with Missing Values in Pandas

Hey there! Nice work on those two initial approaches to identify columns with missing values in your Pandas DataFrame. Let’s dive into some more concise, idiomatic, and efficient methods that are widely used in the Pandas ecosystem:


1. Use isnull().any() with Boolean Indexing

This is the most concise and efficient method if you only need to know which columns have missing values (not how many). any() stops checking a column as soon as it finds the first missing value, making it faster for large datasets:

req_col = df.columns[df.isnull().any()].tolist()

How it works:

  • df.isnull() creates a boolean DataFrame where each cell is True if the value is missing.
  • .any() returns a boolean Series (one value per column) indicating if the column has at least one missing value.
  • We use this boolean Series to index df.columns, then convert the result to a list.

2. Leverage isna().sum() for Missing Value Counts + Column List

If you also want to know how many missing values are in each column (useful for further analysis), you can combine sum() with boolean indexing:

# First get the count of missing values per column
missing_counts = df.isna().sum()
# Filter columns where count > 0 and convert to list
req_col = missing_counts[missing_counts > 0].index.tolist()

Or as a one-liner:

req_col = df.columns[df.isna().sum() > 0].tolist()

Note: isna() and isnull() are completely interchangeable in Pandas—use whichever you find more readable.


3. Filter Directly from the Missing Value Count Series

Another clean approach is to work with the Series of missing value counts directly, which lets you easily access both the column names and their missing value counts:

# Get a Series of columns with non-zero missing values
missing_cols = df.isnull().sum()[lambda x: x > 0]
# Extract just the column names as a list
req_col = missing_cols.index.tolist()
# Bonus: Print the counts if needed
print(missing_cols)

Quick Comparison of Methods

  • Your original loop/list comprehension: Great for learning the underlying logic, but more verbose and less efficient than Pandas' built-in vectorized operations.
  • isnull().any() method: Best for speed and simplicity when you only need column names.
  • isna().sum() methods: Ideal when you want to analyze missing value counts alongside column names.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:42:34