Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Append data to an empty Pandas DataFrame
Next article icon

Pandas Append Rows & Columns to Empty DataFrame

Last Updated : 19 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Python
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:

Append-Rows-Columns-to-Empty-DataFrame

Append Rows & Columns to Empty DataFrame

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.

Python
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.

Python
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.

Python
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:

MethodDescription
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. 

Python
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:

Pandas Append Rows & Columns to Empty DataFrame

Appending Rows and Columns to Empty DataFrame

Example 2: This method will create a new Dataframe with a new column added to the old Dataframe using assign in Pandas.

Python
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: 

Pandas Append Rows & Columns to Empty DataFrame

Appending Rows and Columns to Empty DataFrame

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. 

Python
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: 

Pandas Append Rows & Columns to Empty DataFrame


Next Article
Append data to an empty Pandas DataFrame
author
ankthon
Improve
Article Tags :
  • AI-ML-DS
  • Pandas
  • AI-ML-DS With Python
  • Python pandas-dataFrame
  • Python-pandas

Similar Reads

  • How to add Empty Column to Dataframe in Pandas?
    In Pandas we add empty columns to a DataFrame to create placeholders for future data or handle missing values. We can assign empty columns using different methods depending on the type of placeholder value we want. In this article, we will see different methods to add empty columns and how each one
    2 min read
  • Append data to an empty Pandas DataFrame
    Let us see how to append data to an empty Pandas DataFrame. Creating the Data Frame and assigning the columns to it # importing the module import pandas as pd # creating the DataFrame of int and float a = [[1, 1.2], [2, 1.4], [3, 1.5], [4, 1.8]] t = pd.DataFrame(a, columns =["A", "B
    2 min read
  • Pandas DataFrame transpose() Method - Swap Rows and Columns
    Transposing a Pandas DataFrame means switching rows and columns. That means row labels become column headers and column headers become row labels. It is useful when we want to change orientation of our data for better readability and analysis. In this article, we will see some examples to understand
    2 min read
  • Add zero columns to Pandas Dataframe
    Prerequisites: Pandas The task here is to generate a Python program using its Pandas module that can add a column with all entries as zero to an existing dataframe. A Dataframe is a two-dimensional, size-mutable, potentially heterogeneous tabular data.It is used to represent data in tabular form lik
    2 min read
  • How to get column and row names in DataFrame?
    While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations.  Note: For downloading the nba dataset used in the below examples Click Here  Getting row names in Pandas dataframe First, let's
    4 min read
  • Add multiple columns to dataframe in Pandas
    In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe.  Add multiple columns to a DataFrame using Lists[GFGTABS] Python3 # importing pandas library import pandas as pd # creating and initializing a nested list stud
    3 min read
  • Add column names to dataframe in Pandas
    Sometimes, Pandas DataFrames are created without column names, or with generic default names (like 0, 1, 2, etc.). Let's learn how to add column names to DataFrames in Pandas. Adding Column Names Directly to columns Attribute The simplest way to add column names is by directly assigning a list of co
    3 min read
  • Show all columns of Pandas DataFrame
    Pandas sometimes hides some columns by default if the DataFrame is too wide. To view all the columns in a DataFrame pandas provides a simple way to change the display settings using the pd.set_option() function. This function allow you to control how many rows or columns are displayed in the output.
    2 min read
  • Count number of rows and columns in Pandas dataframe
    In Pandas understanding number of rows and columns in a DataFrame is important for knowing structure of our dataset. Whether we're cleaning the data, performing calculations or visualizing results finding shape of the DataFrame is one of the initial steps. In this article, we'll explore various ways
    3 min read
  • How to Append Pandas DataFrame to Existing CSV File?
    In this discussion, we'll explore the process of appending a Pandas DataFrame to an existing CSV file using Python. Add Pandas DataFrame to an Existing CSV File. To achieve this, we can utilize the to_csv() function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mo
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences