열과 행에 apply() 적용하기
axis=0과 axis=1을 사용하는 DataFrame.apply()로 각 열 또는 각 행에 사용자 정의 함수를 실행합니다.
열과 행에 apply() 적용하기은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
When to Use apply()
DataFrame.apply() runs a custom Python function over each column (axis=0) or each row (axis=1). It is the tool of last resort — slower than built-in vectorised operations — but essential when a computation cannot be expressed with NumPy arithmetic, boolean indexing, or built-in aggregation functions. Use it when you need complex conditional logic, custom string parsing, or multi-step calculations that operate on a single row or column at a time.
import pandas as pd
import numpy as np
df = pd.DataFrame({
'price': [10.5, 25.0, 8.3, 100.0],
'quantity': [3, 1, 5, 2],
'tax_rate': [0.05, 0.10, 0.05, 0.15]
})
print(df)apply() Along Columns (axis=0)
With axis=0 (the default), apply() passes each column as a Series to the function. The function receives one column at a time and should return a scalar (for aggregation) or a Series (for transformation). This is useful for applying a custom normalisation or cleaning step uniformly to all numeric columns in one call.
# Custom range normalisation (0 to 1) for each column
def min_max_scale(col):
return (col - col.min()) / (col.max() - col.min())
df_scaled = df[['price', 'quantity']].apply(min_max_scale, axis=0)
print(df_scaled)apply() Along Rows (axis=1)
With axis=1, apply() passes each row as a Series to the function. The row's index is the column name, so you can access values by name. This is ideal for row-level calculations that depend on multiple columns, such as computing total revenue after tax where each row has its own tax rate.
def revenue_after_tax(row):
subtotal = row['price'] * row['quantity']
return subtotal * (1 + row['tax_rate'])
df['revenue_after_tax'] = df.apply(revenue_after_tax, axis=1)
print(df)Returning Multiple Values with apply()
A function passed to apply(axis=1) can return a pd.Series with named entries, which Pandas expands into multiple new columns. This is cleaner than calling apply() twice for two new columns. Alternatively, the function can return a dict, which Pandas also expands into columns.
def compute_totals(row):
subtotal = row['price'] * row['quantity']
tax = subtotal * row['tax_rate']
return pd.Series({'subtotal': subtotal, 'tax': tax, 'total': subtotal + tax})
result = df.apply(compute_totals, axis=1)
print(result)apply() for Custom Aggregation on Columns
Use apply(func, axis=0) to compute a custom statistic for each column and return a summary Series. For example, compute the coefficient of variation (standard deviation divided by mean) for every numeric column in one call. The result is a Series indexed by column name, which is useful for quick per-column diagnostics.
def coeff_of_variation(col):
return col.std() / col.mean() if col.mean() != 0 else np.nan
cv = df[['price', 'quantity']].apply(coeff_of_variation, axis=0)
print('Coefficient of variation per column:')
print(cv)Comparing apply() to Vectorised Alternatives
Before using apply(), always check whether a vectorised alternative exists. For the revenue-after-tax calculation, df['price'] * df['quantity'] * (1 + df['tax_rate']) does the same thing as the apply(axis=1) version but runs 10–100x faster because it uses NumPy C-level loops. Reserve apply() for cases where vectorised logic would be unreadably complex.
import time
start = time.time()
df['vectorised'] = df['price'] * df['quantity'] * (1 + df['tax_rate'])
print('Vectorised:', time.time() - start, 's')
start = time.time()
df['apply_result'] = df.apply(revenue_after_tax, axis=1)
print('apply():', time.time() - start, 's')apply() with a Lambda
For short one-line transformations, pass a lambda directly to apply() instead of defining a named function. Lambdas are concise for simple operations but should be avoided for complex logic where a named function with comments is easier to understand and test. Use lambdas sparingly — they cannot be easily unit-tested or profiled by name.
# Clip revenue between 0 and 200, then round to nearest 10
df['revenue_clean'] = df['revenue_after_tax'].apply(lambda x: round(min(max(x, 0), 200) / 10) * 10)
print(df[['revenue_after_tax', 'revenue_clean']])Passing Extra Arguments to apply()
Pass additional arguments to your function using the args tuple or keyword arguments in apply(func, args=(val,), axis=1). This avoids using global variables inside the function and makes the function reusable with different parameter values. In Python 3.8+, you can also use functools.partial to create a partially applied version of the function.
def discount_price(row, discount_pct, threshold):
if row['quantity'] >= threshold:
return row['price'] * (1 - discount_pct)
return row['price']
df['discounted_price'] = df.apply(
discount_price, axis=1,
args=(0.1, 3) # 10% discount for quantity >= 3
)
print(df[['price', 'quantity', 'discounted_price']])apply() for Type Conversion
When a column contains mixed types — some integers, some floats, some strings — astype() raises an error. Use apply(pd.to_numeric, errors='coerce') to attempt conversion row by row and return NaN for values that cannot be converted. This is a safe pattern for columns that should be numeric but have occasional non-numeric entries from data entry errors.
messy = pd.Series(['10', '20.5', 'N/A', '100', '--'])
clean = messy.apply(pd.to_numeric, errors='coerce')
print(clean)Performance: apply() vs np.vectorize
When a function that applies to scalars must be applied element-wise, np.vectorize(func)(array) is typically faster than Series.apply(func) because NumPy's vectorize dispatches through C rather than Python's function call overhead. However, np.vectorize is still slower than true broadcasting — it is only useful when no broadcasting expression captures the logic.
def classify_size(price):
if price < 15:
return 'small'
elif price < 50:
return 'medium'
return 'large'
# np.vectorize approach
classify_v = np.vectorize(classify_size)
df['size_class'] = classify_v(df['price'].values)
print(df[['price', 'size_class']])When apply() Is the Right Choice
Apply is the right tool when: (1) the logic requires branching on multiple column values simultaneously; (2) the function calls an external API or database per row (unavoidably sequential); (3) the function parses a complex string like a JSON blob stored in a cell; or (4) the function returns a variable-length object like a list. In all other cases, prefer vectorised operations for speed and readability.
import json
# Parse a JSON metadata column row by row
df['metadata'] = ['{"color": "red"}', '{"color": "blue"}', '{}', '{"color": "green"}']
df['color'] = df['metadata'].apply(lambda s: json.loads(s).get('color', 'unknown'))
print(df[['metadata', 'color']])Quick Check
Test your understanding of Data Analysis concepts from this lesson.
Lesson Recap
In this lesson you learned: using apply(axis=0) to compute column-level custom statistics, using apply(axis=1) to compute row-level calculations with multiple column inputs, and recognising when to prefer vectorised alternatives for performance. Next up we explore apply() with GroupBy for complex group-level aggregations.
자주 묻는 질문
“열과 행에 apply() 적용하기” 강의는 무료인가요?
네 — “열과 행에 apply() 적용하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“열과 행에 apply() 적용하기”에서 뭘 배우나요?
axis=0과 axis=1을 사용하는 DataFrame.apply()로 각 열 또는 각 행에 사용자 정의 함수를 실행합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“열과 행에 apply() 적용하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 열과 행에 apply() 적용하기
- GroupBy와 함께 apply() 사용하기
- 요소별 연산에 map()과 applymap() 사용하기
- pipe()를 사용한 메서드 연결