How to create an empty DataFrame and append rows & columns to it in Pandas?

Pandas is a Python library used for data manipulation and analysis. It provides an efficient implementation of a DataFrame - a two-dimensional data structure where data is aligned in rows and columns in tabular form. While data is typically imported from sources like CSV, Excel, or SQL, sometimes you need to create an empty DataFrame and build it programmatically by adding rows and columns.

Creating an Empty DataFrame

You can create an empty DataFrame using the pd.DataFrame() constructor ?

import pandas as pd

# Create completely empty DataFrame
df = pd.DataFrame()
print("Empty DataFrame:")
print(df)
print(f"Shape: {df.shape}")
Empty DataFrame:
Empty DataFrame
Columns: []
Index: []
Shape: (0, 0)

Creating Empty DataFrame with Column Names

It's often better to define column names upfront ?

import pandas as pd

# Create empty DataFrame with predefined columns
df = pd.DataFrame(columns=['Name', 'Age', 'City'])
print("Empty DataFrame with columns:")
print(df)
print(f"Shape: {df.shape}")
Empty DataFrame with columns:
Empty DataFrame
Columns: [Name, Age, City]
Index: []
Shape: (0, 3)

Appending Rows Using pd.concat()

Use pd.concat() to add rows to your DataFrame ?

import pandas as pd

# Create empty DataFrame with columns
df = pd.DataFrame(columns=['Name', 'Age', 'City'])

# Append rows using pd.concat()
new_row1 = pd.DataFrame([['Alice', 25, 'New York']], columns=['Name', 'Age', 'City'])
new_row2 = pd.DataFrame([['Bob', 30, 'London']], columns=['Name', 'Age', 'City'])
new_row3 = pd.DataFrame([['Charlie', 35, 'Tokyo']], columns=['Name', 'Age', 'City'])

df = pd.concat([df, new_row1, new_row2, new_row3], ignore_index=True)
print(df)
      Name  Age      City
0    Alice   25  New York
1      Bob   30    London
2  Charlie   35     Tokyo

Appending Multiple Rows at Once

You can append multiple rows in a single operation ?

import pandas as pd

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

# Append multiple rows at once
new_data = pd.DataFrame([
    ['Laptop', 999.99, 50],
    ['Mouse', 29.99, 200],
    ['Keyboard', 79.99, 150]
], columns=['Product', 'Price', 'Stock'])

df = pd.concat([df, new_data], ignore_index=True)
print(df)
   Product   Price  Stock
0   Laptop  999.99     50
1    Mouse   29.99    200
2 Keyboard   79.99    150

Adding New Columns

Add columns by direct assignment or using pd.Series ?

import pandas as pd

# Start with existing DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35]
})

# Add column using direct assignment
df['Salary'] = [50000, 60000, 70000]

# Add column using pd.Series
df['Department'] = pd.Series(['IT', 'HR', 'Finance'], index=df.index)

print(df)
      Name  Age  Salary Department
0    Alice   25   50000         IT
1      Bob   30   60000         HR
2  Charlie   35   70000    Finance

Complete Example: Building a DataFrame Step by Step

Here's a comprehensive example showing the complete process ?

import pandas as pd

# Step 1: Create empty DataFrame with columns
df = pd.DataFrame(columns=['Student', 'Math', 'Science'])

# Step 2: Add rows one by one
students_data = [
    ['John', 85, 92],
    ['Sarah', 90, 88],
    ['Mike', 78, 85]
]

for student_data in students_data:
    new_row = pd.DataFrame([student_data], columns=['Student', 'Math', 'Science'])
    df = pd.concat([df, new_row], ignore_index=True)

# Step 3: Add new columns
df['Average'] = (df['Math'] + df['Science']) / 2
df['Grade'] = ['A' if avg >= 85 else 'B' for avg in df['Average']]

print("Final DataFrame:")
print(df)
Final DataFrame:
 Student  Math  Science  Average Grade
0    John    85       92     88.5     A
1   Sarah    90       88     89.0     A
2    Mike    78       85     81.5     B

Key Parameters

Parameter Function Description
ignore_index=True pd.concat() Resets index after concatenation
columns pd.DataFrame() Defines column names
index pd.Series() Aligns series with DataFrame index

Conclusion

Create empty DataFrames using pd.DataFrame() with predefined columns. Use pd.concat() with ignore_index=True to append rows, and direct assignment to add columns. This approach is useful for building DataFrames programmatically when data arrives incrementally.

Updated on: 2026-03-27T06:29:49+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements