0Pricing
Pandas & NumPy Academy · 강의

DataFrame 쌓기에 pd.concat 사용하기

pd.concat으로 DataFrame을 세로 또는 가로로 쌓고 인덱스나 열을 기준으로 맞추며 중복 인덱스를 처리합니다.

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

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

Why Concatenate DataFrames?

In real projects, data rarely arrives in a single file. You might have monthly sales files, regional survey exports, or database query results split across multiple queries. Concatenation stacks these separate DataFrames into one combined table. Pandas provides pd.concat() for this purpose, handling both vertical (row-wise) and horizontal (column-wise) stacking.

Basic Vertical Concatenation

The most common use of pd.concat() is stacking DataFrames vertically (adding more rows). Pass a list of DataFrames and the function appends them one below the other. Both DataFrames must have compatible columns for a clean result. Pandas aligns on column names automatically, filling missing columns with NaN.

import pandas as pd

jan = pd.DataFrame({'product': ['A', 'B'], 'sales': [100, 200]})
feb = pd.DataFrame({'product': ['A', 'C'], 'sales': [150, 120]})

combined = pd.concat([jan, feb])
print(combined)
#   product  sales
# 0       A    100
# 1       B    200
# 0       A    150
# 1       C    120

Resetting the Index After Concat

Notice that pd.concat() preserves the original indices from each DataFrame, which can result in duplicate index values (as seen above with two rows at index 0 and index 1). Pass ignore_index=True to create a fresh sequential integer index in the combined result, which is almost always what you want.

combined = pd.concat([jan, feb], ignore_index=True)
print(combined)
#   product  sales
# 0       A    100
# 1       B    200
# 2       A    150
# 3       C    120

print(combined.index)
# RangeIndex(start=0, stop=4, step=1)

Tracking Source with keys

When concatenating data from multiple sources, you may want to know which original DataFrame each row came from. Pass the keys parameter with a list of labels. Pandas creates a MultiIndex where the outer level identifies the source. You can then use .loc['label'] to access rows from a specific source.

combined = pd.concat([jan, feb], keys=['January', 'February'])
print(combined)
#                product  sales
# January   0         A    100
#           1         B    200
# February  0         A    150
#           1         C    120

# Access only February rows
print(combined.loc['February'])

Horizontal Concatenation with axis=1

Pass axis=1 to concatenate DataFrames side by side (adding more columns). Pandas aligns on the row index, so both DataFrames should have the same index for a clean result. If indices differ, non-matching rows will be filled with NaN. This is useful for combining features computed from the same dataset in separate steps.

names = pd.DataFrame({'name': ['Alice', 'Bob', 'Carol']}, index=[1, 2, 3])
scores = pd.DataFrame({'score': [95, 88, 72]}, index=[1, 2, 3])

combined = pd.concat([names, scores], axis=1)
print(combined)
#     name  score
# 1  Alice     95
# 2    Bob     88
# 3  Carol     72

Handling Mismatched Columns

If the DataFrames being concatenated have different columns, Pandas keeps all columns and fills missing values with NaN. This is the default join='outer' behaviour. If you want to keep only the columns that appear in all DataFrames, pass join='inner', which drops any column not present in every source.

df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df2 = pd.DataFrame({'b': [5, 6], 'c': [7, 8]})

# Outer join (default): keeps all columns
print(pd.concat([df1, df2], ignore_index=True))
#      a  b    c
# 0  1.0  3  NaN
# 1  2.0  4  NaN
# 2  NaN  5  7.0
# 3  NaN  6  8.0

# Inner join: keeps only shared columns
print(pd.concat([df1, df2], join='inner', ignore_index=True))
#    b
# 0  3
# 1  4
# 2  5
# 3  6

Concatenating Many Files in a Loop

A typical pattern when loading multiple files is to collect all DataFrames in a list and call pd.concat() once at the end. Avoid concatenating inside a loop (e.g., df = pd.concat([df, new_chunk])) because this creates a new DataFrame copy on every iteration, leading to quadratic time complexity for large collections.

import glob

# Efficient: collect first, concat once
files = glob.glob('data/sales_*.csv')
frames = [pd.read_csv(f) for f in files]
combined = pd.concat(frames, ignore_index=True)

# Inefficient (avoid):
# result = pd.DataFrame()
# for f in files:
#     result = pd.concat([result, pd.read_csv(f)])  # slow!

Concatenating Series

pd.concat() works with Series as well as DataFrames. Concatenating a list of Series vertically gives a longer Series. Concatenating with axis=1 produces a DataFrame where each Series becomes a column. The Series are aligned on their index, so the index labels must match for a clean horizontal concat.

s1 = pd.Series([1, 2, 3], name='x')
s2 = pd.Series([4, 5, 6], name='y')

# Vertical: one long Series
print(pd.concat([s1, s2]))
# 0    1
# 1    2
# ...

# Horizontal: a DataFrame with two columns
print(pd.concat([s1, s2], axis=1))
#    x  y
# 0  1  4
# 1  2  5
# 2  3  6

Verifying the Concatenated Result

After concatenating, always verify the result has the expected shape and no unexpected NaN values. A quick sanity check pattern: compare the combined row count to the sum of individual counts, and call isna().sum() to spot any unintended missing values introduced by column misalignment. These checks prevent silent data quality issues from propagating into your analysis.

combined = pd.concat([jan, feb], ignore_index=True)

# Sanity checks
assert len(combined) == len(jan) + len(feb), 'Row count mismatch'
print('Missing values per column:')
print(combined.isna().sum())
print('Shape:', combined.shape)

concat vs append (deprecated)

Older Pandas code may use df.append(other), which was a convenience wrapper around pd.concat(). This method was deprecated in Pandas 1.4 and removed in Pandas 2.0. Always use pd.concat([df, other], ignore_index=True) in modern code. The behaviour is identical but pd.concat is more explicit and supports concatenating more than two DataFrames at once.

# Old (removed in Pandas 2.0):
# combined = jan.append(feb, ignore_index=True)

# Modern equivalent:
combined = pd.concat([jan, feb], ignore_index=True)
print(combined)

Practical Example: Yearly Sales Report

Here is a realistic pattern: load monthly DataFrames, add a source label, concatenate, and compute a full-year summary. The keys parameter makes it easy to trace each row back to its month, and ignore_index=True combined with a month column gives a clean, flat DataFrame for group-by analysis.

months = ['jan', 'feb', 'mar']
frames = []
for m in months:
    df = pd.DataFrame({'product': ['A', 'B'], 'sales': [100, 200]})
    df['month'] = m
    frames.append(df)

yearly = pd.concat(frames, ignore_index=True)
print(yearly.groupby('month')['sales'].sum())

Quick Check

Test your understanding of pd.concat for stacking DataFrames from this lesson.

Lesson Recap

In this lesson you learned: how to stack DataFrames vertically with pd.concat() and ignore_index=True, how to track sources using the keys parameter, and how join='inner' vs 'outer' controls column handling when schemas differ. Next up we explore pd.merge() for SQL-style inner and outer joins on shared key columns.

자주 묻는 질문

“DataFrame 쌓기에 pd.concat 사용하기” 강의는 무료인가요?

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

“DataFrame 쌓기에 pd.concat 사용하기”에서 뭘 배우나요?

pd.concat으로 DataFrame을 세로 또는 가로로 쌓고 인덱스나 열을 기준으로 맞추며 중복 인덱스를 처리합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

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

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

“DataFrame 쌓기에 pd.concat 사용하기” 강의는 얼마나 걸리나요?

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

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

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

이 강의의 모든 강의

  1. DataFrame 쌓기에 pd.concat 사용하기
  2. pd.merge: 내부 조인과 외부 조인
  3. 왼쪽 조인과 오른쪽 조인
  4. 인덱스로 조인하기
← Pandas & NumPy Academy(으)로 돌아가기