0Pricing
Pandas & NumPy Academy · 강의

결측값 감지하기

isna(), notna(), isnull()을 사용해 Series 또는 DataFrame에서 NaN의 위치를 찾고 열별 결측값 개수를 셉니다.

결측값 감지하기은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Are Missing Values in Pandas?

In Pandas, a missing value is represented as NaN (Not a Number) for numeric columns and None or pd.NaT for datetime columns. Missing data is common in real-world datasets because records may be incomplete, sensors may fail, or joins may produce unmatched rows. Detecting missing values is always the first step in any data cleaning workflow.

Pandas normalises None, float('nan'), and numpy.nan to the same internal NaN representation for numeric columns.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'name': ['Alice', None, 'Carol'],
    'age': [25, np.nan, 30],
    'salary': [50000.0, 60000.0, np.nan]
})
print(df)
#     name   age   salary
# 0  Alice  25.0  50000.0
# 1   None   NaN  60000.0
# 2  Carol  30.0      NaN

isna() and isnull()

isna() and isnull() are completely identical — both return a DataFrame or Series of the same shape filled with True wherever the value is missing and False elsewhere. Pandas provides both names purely for user preference. The result can be used directly as a boolean mask for filtering or further computation.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [1, np.nan, 3],
    'B': [np.nan, 2, np.nan]
})

print(df.isna())
#        A      B
# 0  False   True
# 1   True  False
# 2  False   True

# Both are identical
print((df.isna() == df.isnull()).all().all())  # True

notna() to Find Non-Missing Values

notna() (also aliased as notnull()) is the inverse of isna() — it returns True where values are present and False where they are missing. This is useful when you want to filter to rows that have a value in a critical column, such as requiring that a primary key or target variable is not NaN.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'id': [1, 2, 3],
    'email': ['a@x.com', None, 'c@x.com']
})

# Keep only rows where email is present
with_email = df[df['email'].notna()]
print(with_email)
#    id    email
# 0   1  a@x.com
# 2   3  c@x.com

Counting Missing Values per Column

Calling .isna().sum() on a DataFrame sums the True values (which equal 1) column-by-column, giving you the count of missing values per column. This is the single most useful first step in understanding the quality of a new dataset — it tells you which columns need attention.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'name': ['Alice', None, 'Carol', None],
    'age': [25, np.nan, 30, 22],
    'salary': [50000, 60000, np.nan, np.nan]
})

missing_counts = df.isna().sum()
print(missing_counts)
# name      2
# age       1
# salary    2
# dtype: int64

Missing Percentage per Column

An absolute count of NaN values is less useful than the percentage of missing values because it scales with dataset size. Dividing isna().sum() by the total row count (or calling isna().mean()) gives the fraction missing, which you can multiply by 100 for a percentage. Columns with more than 30-50% missing often require a decision about whether to keep them at all.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [1, np.nan, 3, np.nan, 5],
    'B': [np.nan, 2, np.nan, 4, 5],
    'C': [1, 2, 3, 4, 5]
})

missing_pct = (df.isna().mean() * 100).round(1)
print(missing_pct)
# A    40.0
# B    40.0
# C     0.0
# dtype: float64

Missing Summary Table

A common EDA pattern is to build a missing value summary table that shows count, percentage, and dtype for each column in one view. This gives a complete picture of data quality before making any cleaning decisions. You can sort it to surface the most problematic columns first.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'col_a': [1, np.nan, 3],
    'col_b': [np.nan, np.nan, 3],
    'col_c': [1, 2, 3]
})

summary = pd.DataFrame({
    'missing_count': df.isna().sum(),
    'missing_pct': (df.isna().mean() * 100).round(1),
    'dtype': df.dtypes
}).sort_values('missing_pct', ascending=False)
print(summary)
#         missing_count  missing_pct   dtype
# col_b               2         66.7  float64
# col_a               1         33.3  float64
# col_c               0          0.0  float64

Row-Level Missing Value Count

You can also count missing values per row by calling isna().sum(axis=1). This helps identify records that are mostly empty (e.g., incomplete survey responses) which you might want to flag or remove as a unit. A row with many missing values is fundamentally different from scattered column-level missingness.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'a': [1, np.nan, np.nan],
    'b': [np.nan, 2, np.nan],
    'c': [3, 4, np.nan]
})

# Count NaN per row
df['missing_count'] = df.isna().sum(axis=1)
print(df)
#      a    b    c  missing_count
# 0  1.0  NaN  3.0              1
# 1  NaN  2.0  4.0              1
# 2  NaN  NaN  NaN              3

Filtering Rows with Any or All Missing

Use df[df.isna().any(axis=1)] to find rows that have at least one NaN value, or df[df.isna().all(axis=1)] to find rows where every value is NaN. These filters help you isolate problem records for inspection before deciding how to handle them.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'x': [1, np.nan, np.nan],
    'y': [2, 3, np.nan],
    'z': [4, 5, np.nan]
})

# Rows with at least one NaN
has_any_nan = df[df.isna().any(axis=1)]
print('Any NaN:\n', has_any_nan)

# Rows where all values are NaN
all_nan = df[df.isna().all(axis=1)]
print('All NaN:\n', all_nan)
#      x    y    z
# 2  NaN  NaN  NaN

Checking a Specific Column for NaN

For a quick sanity check on a single column, call df['col'].isna().sum() or use df['col'].isna().any() to get a single boolean (True if any NaN exists). These one-liners are useful inside data validation checks or logging statements in a pipeline.

import pandas as pd
import numpy as np

df = pd.DataFrame({'price': [10.0, np.nan, 30.0, np.nan, 50.0]})

print('NaN count in price:', df['price'].isna().sum())  # 2
print('Any NaN in price?', df['price'].isna().any())    # True
print('All present?', df['price'].notna().all())         # False

Visualising Missing Values with a Heatmap

For datasets with many columns, a missing value heatmap is more informative than a table of numbers. You can create one easily with Seaborn: the darker a cell, the more missing data in that column-row combination. A popular third-party library called missingno provides dedicated missing-value visualisations.

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

np.random.seed(0)
df = pd.DataFrame(
    np.where(np.random.rand(20, 5) > 0.7, np.nan, np.random.randn(20, 5)),
    columns=['A', 'B', 'C', 'D', 'E']
)

# Heatmap of missing values
sns.heatmap(df.isna(), cbar=False, yticklabels=False)
plt.title('Missing Value Pattern')
plt.tight_layout()
plt.savefig('missing_heatmap.png')
print('Saved missing_heatmap.png')

info() for a Quick Missing Check

df.info() prints a concise summary that includes the non-null count for every column. This is the fastest way to spot columns with missing values in a new dataset: any column whose non-null count is less than the total row count has NaN values. It also shows dtype and memory usage.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'id': [1, 2, 3, 4, 5],
    'age': [25, np.nan, 30, np.nan, 22],
    'salary': [50000, 60000, np.nan, 70000, 80000]
})

df.info()
# <class 'pandas.core.frame.DataFrame'>
# RangeIndex: 5 entries, 0 to 4
# Data columns (total 3 columns):
#  #   Column  Non-Null Count  Dtype
# ---  ------  --------------  -----
#  0   id      5 non-null      int64
#  1   age     3 non-null      float64
#  2   salary  4 non-null      float64

Quick Check

Test your understanding of detecting missing values in Pandas.

Lesson Recap

In this lesson you learned: isna() and isnull() are identical and return boolean masks of missing positions, isna().sum() counts NaN per column, and isna().mean()*100 gives the missing percentage. Use df.info() for a fast overview and isna().any(axis=1) to find rows with at least one NaN. Next up we tackle dropping missing values with dropna().

자주 묻는 질문

“결측값 감지하기” 강의는 무료인가요?

네 — “결측값 감지하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“결측값 감지하기”에서 뭘 배우나요?

isna(), notna(), isnull()을 사용해 Series 또는 DataFrame에서 NaN의 위치를 찾고 열별 결측값 개수를 셉니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“결측값 감지하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 결측값 감지하기
  2. 결측값 삭제하기
  3. 결측값 채우기
  4. 보간과 고급 대치
← Pandas & NumPy Academy(으)로 돌아가기