0
0
PandasHow-ToBeginner · 3 min read

How to Create an Empty DataFrame in pandas

To create an empty DataFrame in pandas, use pd.DataFrame() with no arguments. You can also specify columns by passing a list to the columns parameter, like pd.DataFrame(columns=['A', 'B']).
📐

Syntax

The basic syntax to create an empty DataFrame is pd.DataFrame(). You can optionally add column names by using the columns parameter with a list of column names.

  • pd.DataFrame(): Creates a completely empty DataFrame with no rows and no columns.
  • pd.DataFrame(columns=[...]): Creates an empty DataFrame with specified column names but no rows.
python
import pandas as pd

# Empty DataFrame with no columns and no rows
df_empty = pd.DataFrame()

# Empty DataFrame with columns but no rows
df_with_columns = pd.DataFrame(columns=['Name', 'Age'])
Output
print(df_empty) # Empty DataFrame # Columns: [] # Index: [] print(df_with_columns) # Empty DataFrame # Columns: [Name, Age] # Index: []
💻

Example

This example shows how to create an empty DataFrame with columns and then add data later. It demonstrates the empty structure and how it looks when printed.

python
import pandas as pd

# Create empty DataFrame with columns
empty_df = pd.DataFrame(columns=['Product', 'Price'])

print(empty_df)

# Add a row later
empty_df.loc[0] = ['Apple', 0.99]

print(empty_df)
Output
Empty DataFrame Columns: [Product, Price] Index: [] Product Price 0 Apple 0.99
⚠️

Common Pitfalls

One common mistake is trying to create an empty DataFrame by passing None or empty lists incorrectly, which can cause errors or unexpected results. Another is forgetting to specify columns when you want a DataFrame with a structure but no data.

Always use pd.DataFrame() for a fully empty DataFrame or pd.DataFrame(columns=[...]) to define columns upfront.

python
import pandas as pd

# Wrong: Passing None causes error
# df_wrong = pd.DataFrame(None)  # This raises an error

# Right: Passing empty list creates empty DataFrame with no rows and no columns
# df_wrong2 = pd.DataFrame([])

# Right: Use empty DataFrame syntax

df_correct = pd.DataFrame()
df_with_cols = pd.DataFrame(columns=['A', 'B'])
Output
print(df_correct) # Empty DataFrame # Columns: [] # Index: [] print(df_with_cols) # Empty DataFrame # Columns: [A, B] # Index: []
📊

Quick Reference

Here is a quick summary of how to create empty DataFrames in pandas:

MethodDescriptionExample
Empty DataFrameNo rows, no columnspd.DataFrame()
Empty with columnsNo rows, specified columnspd.DataFrame(columns=['col1', 'col2'])
Empty with indexNo columns, specified indexpd.DataFrame(index=[0,1])

Key Takeaways

Use pd.DataFrame() to create a completely empty DataFrame with no rows or columns.
Specify columns with the columns parameter to create an empty DataFrame with a structure.
Avoid passing None or empty lists incorrectly to pd.DataFrame to prevent errors.
You can add rows later to an empty DataFrame using loc or append methods.
Printing an empty DataFrame shows its columns and index clearly, helping verify its structure.