Pandas Append Rows & Columns to Empty DataFrame
Appending rows and columns to an empty DataFrame in pandas is useful when you want to incrementally add data to a table without predefining its structure. To immediately grasp the concept, here’s a quick example of appending rows and columns to an empty DataFrame using the concat()
method, which is frequently used and highly efficient.
import pandas as pd
df = pd.DataFrame() # an empty DataFrame
# Append columns by directly assigning values
df['Name'] = ['Alice', 'Bob']
df['Age'] = [30, 22] # appending columns
# Create new rows as separate DataFrames
new_row1 = pd.DataFrame({'Name': ['Charlie'], 'Age': [28]})
new_row2 = pd.DataFrame({'Name': ['David'], 'Age': [35]})
df = pd.concat([df, new_row1, new_row2], ignore_index=True) # Appending new rows using concat()
display(df)
import pandas as pd
df = pd.DataFrame() # an empty DataFrame
# Append columns by directly assigning values
df['Name'] = ['Alice', 'Bob']
df['Age'] = [30, 22] # appending columns
# Create new rows as separate DataFrames
new_row1 = pd.DataFrame({'Name': ['Charlie'], 'Age': [28]})
new_row2 = pd.DataFrame({'Name': ['David'], 'Age': [35]})
df = pd.concat([df, new_row1, new_row2], ignore_index=True) # Appending new rows using concat()
display(df)
Output:
In this example:
- Columns are added directly by assigning lists to column names.
- Rows are appended using the
concat()
method, which is efficient and recommended for appending rows.
There are multiple ways to append rows and columns to an empty Pandas DataFrame, here we will implement each of them.
Using Loc for Appending Rows to an Empty DataFrame
The concat()
function is the recommended method for appending rows, especially since append()
has been deprecated since Pandas version 2.0. We have already seen an example using concat. Now Using loc[]
for Rows, that allows to append rows by specifying the index explicitly.
import pandas as pd
# Create an empty DataFrame with column names
df = pd.DataFrame(columns=['Name', 'Age'])
# Append rows using loc[]
df.loc[0] = ['Alice', 30]
df.loc[1] = ['Bob', 22]
print(df)
import pandas as pd
# Create an empty DataFrame with column names
df = pd.DataFrame(columns=['Name', 'Age'])
# Append rows using loc[]
df.loc[0] = ['Alice', 30]
df.loc[1] = ['Bob', 22]
print(df)
Output:
Name Age
0 Alice 30
1 Bob 22
Appending Columns to an Empty DataFrame
Using Direct Assignment for Columns is the simplest and most frequently used method to append columns to a DataFrame, as demonstrated in the first example. Now, let's look at other methods for appending columns dynamically to a DataFrame.
1. Using assign()
for Columns
The assign()
method allows you to add one or more columns to a DataFrame in a chainable manner.
import pandas as pd
df = pd.DataFrame()
# Append columns using assign()
df = df.assign(Name=['Alice', 'Bob'], Age=[30, 22])
print(df)
Output:
Name Age
0 Alice 30
1 Bob 22
2. Using insert()
for Columns
The insert()
method allows you to add a column at a specific position in the DataFrame.
import pandas as pd
# Create an empty DataFrame with some columns
df = pd.DataFrame({'Name': ['Alice', 'Bob']})
# Insert a new column at position 1 (second column)
df.insert(1, 'Age', [30, 22])
print(df)
import pandas as pd
# Create an empty DataFrame with some columns
df = pd.DataFrame({'Name': ['Alice', 'Bob']})
# Insert a new column at position 1 (second column)
df.insert(1, 'Age', [30, 22])
print(df)
Output:
Name Age
0 Alice 30
1 Bob 22
Summary: Adding Rows and Columns to an Empty DataFrame:
Method | Description |
---|---|
Direct Assignment (Columns) | Easiest way to append columns by assigning values directly. |
assign() (Columns) | Allows adding multiple columns in a chainable manner; returns a new DataFrame. |
insert() (Columns) | Adds a column at a specific position in the DataFrame. |
concat() (Rows) | Efficiently appends rows by concatenating two or more DataFrames. |
loc[] (Rows) | Appends rows by specifying the index and assigning values directly. |
Appending Rows and Columns to Empty DataFrame : Practical Examples
Example 1: Create a complete empty DataFrame without any column name or indices and then append columns in Pandas one by one to it.
import pandas as pd
df = pd.DataFrame()
print(df)
# append columns to an empty DataFrame
df['Name'] = ['Ankit', 'Ankita', 'Yashvardhan']
df['Articles'] = [97, 600, 200]
df['Improved'] = [2200, 75, 100]
print(df)
Output:

Example 2: This method will create a new Dataframe with a new column added to the old Dataframe using assign in Pandas.
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],'Height': [5.1, 6.2, 5.1, 5.2],'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
df = pd.DataFrame(data)
# Using 'Address' as the column name and equating it to the list
df2 = df.assign(address=['Delhi', 'Bangalore', 'Chennai', 'Patna'])
print(df2)
import pandas as pd
# Define a dictionary containing Students data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],'Height': [5.1, 6.2, 5.1, 5.2],'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
df = pd.DataFrame(data)
# Using 'Address' as the column name and equating it to the list
df2 = df.assign(address=['Delhi', 'Bangalore', 'Chennai', 'Patna'])
print(df2)
Output:

Example 3: Create an empty DataFrame with a column name and indices and then append rows one by one to it using the loc[] method.
import pandas as pd
# create an Empty DataFrame object With column names and indices
df = pd.DataFrame(columns = ['Name', 'Articles', 'Improved'],
index = ['a', 'b', 'c'])
print("Empty DataFrame With NaN values : \n\n", df)
# adding rows to an empty dataframe at existing index
df.loc['a'] = ['Ankita', 50, 100]
df.loc['b'] = ['Ankit', 60, 120]
df.loc['c'] = ['Harsh', 30, 60]
print(df)
Output:
