logo

How to Make an Empty DataFrame in Pandas 📂Programing

How to Make an Empty DataFrame in Pandas

Description

When collecting data with pandas or processing data iteratively via loops, it is common to construct an empty DataFrame first and then append rows incrementally. To create a completely empty DataFrame, simply call pd.DataFrame(). If you want to define the DataFrame’s structure in advance by setting only the column names, pass the desired column names as a list to the columns argument.

Code

import pandas as pd

# 1. 완전히 빈 데이터프레임 생성
df_empty = pd.DataFrame()
print(df_empty)
# Empty DataFrame
# Columns: []
# Index: []

# 2. 컬럼명만 지정된 빈 데이터프레임 생성
df_cols = pd.DataFrame(columns=['A', 'B', 'C'])
print(df_cols)
# Empty DataFrame
# Columns: [A, B, C]
# Index: []

Environment

  • Windows 11
  • Python 3.10.11, pandas 2.2.3